简体   繁体   中英

'Unexpected end of JSON input'

Yesterday it was working perfectly fine and I wasn't getting this error.

Tried replacing a bunch of stuff with this one single line and I'm just flat out confused on why this is occurring. I've been researching this for an hour and all the results for 'Unexpected end of JSON input' don't really fully match what I'm doing and are incredibly confusing to me as I am fairly new to javascript.

Line that's causing it:

let userData = JSON.parse(fs.readFileSync(__dirname + '/Storage/userData.json', 'utf8'))

I should also point out that above the error it says what is causing it is 'undefined:1'??? So that's pretty weird. Normally it would show like the line in the javascript code but no, I had to search through the list of "at (something in some script)" to find what line was causing it, I have a feeling it might have to do with the JSON itself instead of my code.

The error is from the ./Storage/userData.json file. Do you have an empty file? If yes, make sure to save it with [] . Sometimes, the file can be cleared by a code error who affects the data.

This happens when your .json file is empty. This is why I do not recomment sorting data in a .json file, if you edit it alot, it can sometimes be completely wiped out. To fix it, simply put {} in the file and the error should be gone. Your data will be gone, too though.

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