![](/img/trans.png)
[英]My code for Discord.JS is having a strange error, can you please help me solve this?
[英]“Undefined” Error in discord.js, can somebody help me?
我开始与discord.js
在Discord中制作一个RPG游戏,但是我收到了一个错误。 这是我的代码和错误:
let exp = require("./exp.json");
let userEXP = exp[message.author.id].exp;
if (voidCost) {
exp[message.author.id] = {
exp: exp[message.author.id].exp - 1700
}
fs.writeFile("./exp.json", JSON.stringify(exp), (err) => {
if (err) {
console.log(err)
}
})
}
let Void = require(`./weapons/Void.json`);
if (voidAmt) {
Void[message.author.id] = {
Void: Void[message.author.id].Void + 3
}
fs.writeFile("./weapons/Void.json", JSON.stringify(Void), (err) => {
if (err) {
console.log(err)
}
})
}
if (!Void[message.author.id]) {
Void[message.author.id] = {
Void: 0
}
}
错误:
(node:8632) UnhandledPromiseRejectionWarning: TypeError:
Cannot read property 'Void' of undefined` (referred to "Void[message.author.id].Void")
有人能帮助我吗? 错误意味着什么,我该如何避免?
尝试这样做:
let Void = require(`./weapons/Void.json`);
if (voidAmt) {
if (!Void[message.author.id]) {
Void[message.author.id] = {
Void: 0
}
}
Void[message.author.id] = {
Void: Void[message.author.id].Void + 3
}
fs.writeFile("./weapons/Void.json", JSON.stringify(Void), (err) => {
if (err) {
console.log(err)
}
})
}
所以基本上只需要检查一下。
只是说使用JSON作为数据库不是很可靠,可能会损坏您的数据。 一个很好的选择是https://www.npmjs.com/package/enmap ,至少有点失败,并且更容易使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.