javascriptnode.jsdiscord.jsdisco

ReferenceError: err is not defined I couldn't solve the error


my codes as below why am i getting such an error ;

" ReferenceError: err is not defined "

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

}


Solution

  • If you are doing error handling you need to detect the error first, try using try{} and catch(){}

    for your code

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