简体   繁体   English

discord.js中的“未定义”错误,有人可以帮助我吗?

[英]“Undefined” Error in discord.js, can somebody help me?

I started making an RPG Game in Discord with discord.js , but I got an error. 我开始与discord.js在Discord中制作一个RPG游戏,但是我收到了一个错误。 Here is my code and the error: 这是我的代码和错误:

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
    }
}

The error: 错误:

(node:8632) UnhandledPromiseRejectionWarning: TypeError:
Cannot read property 'Void' of undefined` (referred to "Void[message.author.id].Void")

Can somebody help me? 有人能帮助我吗? What does the error mean and how can I avoid it? 错误意味着什么,我该如何避免?

Try doing it like this: 尝试这样做:

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)
        }
    })
}

So basically just move the check up. 所以基本上只需要检查一下。

Just saying using JSON for a Database is not very reliable and may corrupt your data. 只是说使用JSON作为数据库不是很可靠,可能会损坏您的数据。 A good alternative would be https://www.npmjs.com/package/enmap , wich is atleast somewhat failsave and has easier useabilitty 一个很好的选择是https://www.npmjs.com/package/enmap ,至少有点失败,并且更容易使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM