简体   繁体   中英

JSON.parse is giving error

I have written a chunk of code.

function getScreenshotObj (pathToFirstFile) {
    return new Promise ((resolve,reject) =>{
        console.log("Path to temp dir : " + pathToFirstFile)
        fs.readFile(pathToFirstFile,function(err,fileContents){
            if (err) {
                return reject(err)
            }
            else{
                screenshotObject = JSON.parse(fileContents)
                obj = {pathToFirstFile : pathToFirstFile , screenshotObject:screenshotObject ,accesstoken : accesstoken}
                return resolve(obj)
            }
        })
    })
}

It's giving me an error at JSON.parse(). Uncaught syntax error : Unexpected end of input at JSON.parse().I checked the syntax using online JS syntax and they said code is syntactically valid. Please correct me where I am making mistake.

For fs.readFile if you don't specify encoding than raw data buffer is returned. source

Change:

fs.readFile(pathToFirstFile, function(err, fileContents) {

to

fs.readFile(pathToFirstFile, 'utf8', function(err, fileContents) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM