[英]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.