ReferenceError:未定义err我无法解决错误

时间:2020-05-23 00:21:05

标签: javascript node.js discord.js disco

我的代码如下,为什么会出现这样的错误;

“ ReferenceError:未定义err”

else if (processMethod.toUpperCase() === "COVID") {
    console.log("white", "get covid statics...");
    targetCountry = (await covid.parseCountry(UK)).countryid;
    if (err.message === "Failed to get country." || err.message === "Invalid Country Code") {

        const ErrMsg2 = new Discord.RichEmbed()
            ErrMsg2.setColor(0x00AE86)
            ErrMsg2.setTimestamp()
            ErrMsg2.setAuthor(message.author.username, message.author.avatarURL)
            ErrMsg2.setThumbnail(message.guild.iconURL)
            ErrMsg2.setDescription(`Failed Try Again`)
            ErrMsg2.setFooter("COVID SERVICES")
            message.channel.sendEmbed(ErrMsg2);
    }

}

1 个答案:

答案 0 :(得分:1)

如果要执行错误处理,则需要首先检测错误,请尝试使用try{}catch(){}

输入您的代码

try {
   targetCountry = (await covid.parseCountry(UK)).countryid;
}
catch (err) {
    if (err.message === "Failed to get country." || err.message === "Invalid Country Code") { 
        // ...
    }
}