ค้นหาเว็บไซต์

node.js - SyntaxError: พบโทเค็น T ที่ไม่คาดคิดใน JSON ที่ตำแหน่ง 0


ฉันกำลังเขียนโปรแกรมแอปพลิเคชันด้วย Typescript ในแอปพลิเคชันนี้ฉันจะอ่านไฟล์ json บางไฟล์ด้วยฟังก์ชัน d3.json แต่โค้ดนี้ใช้ไม่ได้ ข้อผิดพลาดคือ:

SyntaxError: เจอตัวอักษร T โดยไม่คาดคิดใน JSON ที่ตำแหน่ง 0
ที่ JSON.parse()
ที่ Object. (d3-request.node.js:176)
ที่ respond (d3-request.node.js:39)
ที่ exports.XMLHttpRequest.onload.xhr.onreadystatechange (d3-request.node.js:30)
ที่ exports.XMLHttpRequest.dispatchEvent (XMLHttpRequest.js:591)
ที่ setState (XMLHttpRequest.js:610)
ที่ exports.XMLHttpRequest.handleError (XMLHttpRequest.js:532)
ที่ module.exports.errorHandler (XMLHttpRequest.js:459)
ที่ module.exports.EventEmitter.emit (events.js:81)
ที่ request.js:142

รหัสคือดังนี้:

export async function setSelectConfiguration(
filenameJson: string, storeData: (str: string) => void, element: JQuery,
listenerFunction:(e: JQueryEventObject) => void) {
await d3.json("filename2.json", function(error, data) {
    if(error) {
        console.log(error);
    } else {
        console.log(data);
        storeData(data.toString())
    }
});
 element.on("change", listenerFunction);
    }

ไฟล์ json คือแบบนี้:

{
"files": [
{
        "name": "time_sensitive_test.json",
        "type": "user",
        "owner": "Master",
        "data_type": "linear data",
        "description": "Utenti master (Enrica) periodo 20/05/2014-16/10/2014",
    "ids_description": "accesso risorse",
    "ods_description": "chat/forum",
    "bds_description": "login/logout"
}
]}

ไฟล์ gulpfile.js:

var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var tsify = require('tsify');
var sourcemaps = require('gulp-sourcemaps');
var buffer = require('vinyl-buffer');


var paths = {
    pages: ['src/*.html']
};

gulp.task('copyHtml', function () {
    return gulp.src(paths.pages)
        .pipe(gulp.dest('dist'));
   });

   gulp.task('default', ['copyHtml'], function () {
    return browserify({
       basedir: '.',
        debug: true,
        entries: ['src/main.ts'],
       cache: {},
       packageCache: {}
   })
   .plugin(tsify)
    .transform('babelify', {
        presets: ['env','stage-0'],
       extensions: ['.ts'],
        plugins: ['transform-runtime','transform-async-to-generator']
   })
    .bundle()
    .pipe(source('project.js'))
    .pipe(buffer())
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('dist'));
  });

ไฟล์ package.json:

    {
  "name": "project",
  "version": "1.0.0",
  "description": " Project",
  "main": "./dist/project.js",
  "directories": {
    "example": "example"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": " ... "
  },
  "author": "  ",
  "license": "ISC",
  "bugs": {
    "url": "... "
  },
  "homepage": "...",
  "devDependencies": {
    "@types/d3": "^4.10.0",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-stage-0": "^6.24.1",
    "babelify": "^8.0.0",
    "browserify": "^14.5.0",
    "gulp": "^3.9.1",
    "gulp-sourcemaps": "^2.6.3",
    "gulp-typescript": "^3.2.3",
    "gulp-uglify": "^3.0.0",
    "gulp-util": "^3.0.8",
    "tsify": "^3.0.4",
    "typescript": "^2.6.2",
    "vinyl-buffer": "^1.0.1",
    "vinyl-source-stream": "^2.0.0",
    "watchify": "^3.9.0"
  },
  "dependencies": {
    "@types/node": "^8.5.7",
    "babel-polyfill": "^6.26.0",
    "babel-runtime": "^6.26.0",
    "d3": "^4.12.2",
    "webpack": "^3.10.0"
  }
}

ไฟล์ tsconfig.json:

{
"files": [
  ...
],
"compilerOptions": {
    "module": "commonjs",
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true,
    "noImplicitAny": true,
    "target": "es2015",
    "lib": ["es2015", "es2016", "dom", "es2017", "es6", "es5"],
    "experimentalAsyncFunctions": true,
    "typeRoots": [
        "./node_modules/@types"
    ]
},
"exclude": [
    "node_modules",
    "dist"
]
}

ไฟล์ webpack.config.json:

    module.exports = {
    context: __dirname + '/src',
    entry: ['babel-polyfill', './main.ts'],
    output: {
        path: 'dist',
        filename: 'project.js'
    },
    module: {
        loaders: [
            {
                test: /\.js/,
                exclude: /node_modules/,
                loader: 'babel',
                query: {
                    presets: ['es2015']
                }
            }
        ]
    }
};

ฉันรันโค้ดที่คอมไพล์นอก IDE แต่ข้อผิดพลาดก็เกิดขึ้นเหมือนเดิม ดังนั้นมันไม่น่าจะเป็นข้อผิดพลาดแปลก ๆ ของ IDE
ฉันได้ลองอ่านไฟล์ในแอปพลิเคชันจาวาสคริปต์บริสุทธิ์แล้ว ทุกอย่างทำงานได้ดี ดังนั้นปัญหาไม่น่าจะอยู่ที่ไฟล์
ปัญหาอาจเกิดจากอะไรได้บ้าง? ตัวอย่างเช่น: มันต้องการโมดูลพิเศษหรือการตั้งค่าใด ๆ หรือไม่?