【问题标题】:Make Discord Bot for node.js JSON.parse() error : undefined:1为 node.js JSON.parse() 错误制作 Discord Bot:未定义:1
【发布时间】:2018-02-21 00:00:44
【问题描述】:

我想做 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.

我的文件夹

Structure of my folder here

我认为这个错误是这一行

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

还有……

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

这一行。但我不知道如何修理这条线。

我该怎么办?

【问题讨论】:

  • 和.. 我可以链接 youtubeURL 吗?网址是...我参考视频制作这条线

标签: node.js


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    但是,您的错误与此应用程序代码无关:到目前为止还不错!投诉涉及其消耗的 JSON 数据文件,因此出现错误消息:“JSON 输入意外结束”(这是从您的 parse() 方法发出的错误消息)。

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

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

    }
    

    因此,只需在您的userData.json 中添加最后一行就可能会解决您的数据输入文件问题。 (例如,参见this gist 并将您的userData.json 的内容/结构与设置 中描述的config.json 进行比较)

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 2020-02-20
      • 2021-04-16
      • 2019-03-25
      • 2020-11-15
      • 1970-01-01
      • 2022-01-13
      • 2021-10-28
      • 2021-12-20
      相关资源
      最近更新 更多