为node.js创buildDiscord Bot JSON.parse()错误:undefined:1

我想制作Discord Bot。

我使用Node.js和Discord api。

我的错误:

C:\----\----\Desktop\SiiNaBot>node app.js undefined:1 SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at Object.<anonymous> (C:\Users\Lin\Desktop\SiiNaBot\app.js:7:21) at Module._compile (module.js:573:30) at Object.Module._extensions..js (module.js:584:10) at Module.load (module.js:507:32) at tryModuleLoad (module.js:470:12) at Function.Module._load (module.js:462:3) at Function.Module.runMain (module.js:609:10) at startup (bootstrap_node.js:158:16) at bootstrap_node.js:598:3 

我的线:

 //Calling the pakage const Discord = require('discord.js'); const bot = new Discord.Client(); const fs = require('fs'); // JSON Files let userData = JSON.parse(fs.readFileSync('Storage/userData.json', 'utf8')); // This calls the JSON file. //Listener Event : Message Received ( This wiil run every time a message is recived) bot.on('message', message => { //Variables let sender = message.author; // The person who sent th message let msg = message.content.toUpperCase(); // Takes the message, and makes it all uppercase let prefix = '>' // The test before commands, you can set this to what ever you want //Event if(!userData[sender.id + message.guild.id]) userData[sender.id + message.guild.id] = {} // This creates a json file for their user + guild, if one is not made already. if(!userData[sender.id + message.guild.id].money) userData[sender.id + message.guild.id].money = 1000; // This creates a money object for them if they start out with, you can change this to whatever you want. fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => { //This writes the changes we just made to the JSON file. if (err) console.error(err); }) // Commends //Ping if (msg === prefix + 'PING'){ message.channel.send('Pong!') // Sends a message to the chanel, with the contens: "Pong!" } }) // This code runs when the bot turns on bot.on('ready', () => { console.log('Economy Launched...') }) // Login bot.login('I`ll write code my bot token'); //Don`t let people see this code, people can control your bot, including the servers your bot has admin on. 

我的文件夹

我的文件夹的结构在这里

我认为这个错误是这一行

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

和…

fs.writeFile('Storage / userData.json',JSON.stringify(userData),(err)=> {//这会将我们刚刚对JSON文件所做的更改写入。 })

这条线。 但我不知道如何解决这个问题。

我能怎么做?

错误消息是由第一个而不是第二个,因为它试图parsing您的JSON数据。 您可以在错误的调用堆栈中标识此处:

 at Object.<anonymous> (C:\Users\Lin\Desktop\SiiNaBot\app.js:7:21) 

这是在处理你的app.js ,第7行,字符位置21,对应于JSON.parse( ... )方法。

但是,您的错误不在于此应用程序代码:至今为止没有问题! 抱怨涉及JSON数据文件的使用,因此会出现错误消息:“ JSONinput的意外结束”(这是您的parse()方法发出的错误消息)。

此外,它逐行地parsing你的JSON,并且寻找JSON对象定义的结尾,但是这个文件首先结束! 那个JSON文件有什么问题? 那么,缺less的内容就是错误消息的undefined部分,就像调用堆栈中的位置一样:1标识为字符位置1处的缺失内容 – 也就是说,期望在它的游标位于该行的第一个字符处,但是它却会碰到标记。

你的Storage/userData.json JSON对象定义需要包含在你的自定义数据的属性中,因此,我可以肯定你没有终止该JSON对象的定义,最后一行可能缺less的是:

 } 

所以,只要在你的userData.json添加最后一行就可能解决你的数据input文件问题。 (例如,参见这个要点 ,比较你的userData.json的内容/结构和它们在安装程序中描述的config.json