MinuhaYT
MinuhaYT

Reputation: 15

TypeError: Cannot destructure property `body` of 'undefined' or 'null'

I have the following error in the console:

TypeError: Cannot destructure property body of 'undefined' or 'null'.

This is my code:

var { body } = await snekfetch.get(`https://api.roblox.com/users/get-by-username?username=${userArray[i]}`).catch((err) => console.log('caught it'));
        if (body.success === false){
          var errorEmbed = new Discord.RichEmbed()
            .setColor(0xff4040)
            .setDescription(`:warning: **${userArray[i]} doesn't exist on ROBLOX** :warning:`);
            await message.channel.send(errorEmbed);

Upvotes: 1

Views: 1438

Answers (1)

Sohail Ashraf
Sohail Ashraf

Reputation: 10604

Try this.

try {
  var response = await snekfetch.get(
    `https://api.roblox.com/users/get-by-username?username=${userArray[i]}`
  );
  if (response && response.body && response.body.success) {
    var errorEmbed = new Discord.RichEmbed()
      .setColor(0xff4040)
      .setDescription(
        `:warning: **${userArray[i]} doesn't exist on ROBLOX** :warning:`
      );
    await message.channel.send(errorEmbed);
  } else {
    console.log('Error');
  }
} catch (error) {
  console.log(error);
}


Upvotes: 1

Related Questions