提问者:小点点

Make Discord Bot for node.js json.parse()错误:未定义:1


我想制造不和机器人。

我使用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.

我的文件夹

此处得“我得文件夹”结构

我想这个错误就是这一行

让userData=json.parse(fs.readfileSync('storage/userData.json','utf8'))

还有...

fs.writefile('storage/userData.JSON',JSON.stringify(userData),(err)=>{//这将写入我们刚刚对JSON文件所做的更改。if(err)console.error(err);})

这条线。但我不知道怎么才能修好这条绳子。

我该怎么办?


共2个答案

匿名用户

错误消息是从第一个而不是第二个出现的,因为它试图解析JSON数据。您可以在您的错误的调用堆栈中标识这一点:

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

这是在处理app.js的过程中,第7行,字符位置21,它对应于json.parse(...)方法。

然而,您的错误并不在此应用程序代码中:到目前为止,这是正常的!抱怨涉及它所使用的JSON数据文件,因此出现错误消息:“Expected end of JSON Input”(这是从parse()方法发出的错误消息)。

此外,它逐行解析您的JSON,并寻找JSON对象定义的结尾,但文件首先结束!JSON文件中的问题是什么?好的,丢失的内容被报告为错误消息的undefined,就像调用堆栈中的位置一样,:1将其标识为字符位置1处的丢失内容--也就是说,它希望在位于该行第一个字符处的光标处找到一个字符,但却遇到了标记。

storage/userdata.JSON中的JSON对象定义需要用自定义数据包含在属性中,因此,我可以非常肯定您已经省略了终止该JSON对象的定义,最后一行可能缺少的是:

}

因此,只需在userdata.json中添加最后一行就很可能解决数据输入文件的问题。(例如,请参阅此要点,并将userdata.json的内容/结构与安装程序中描述的config.json进行比较)

匿名用户

我使用的教程和你使用的完全一样。有一个非常简单的方法可以解决这个问题。只需输入UserData.json,并输入“{}”,我遇到了完全相同的问题,出现了完全相同的错误消息,这就修复了它。