简体   繁体   中英

discord bot node.js empty array parse problem

here's an API and there's an empty array with the red underline. Sometimes it's empty but sometimes it's not. I am trying to make result to show Item Spells: there are no effects on this item. when the array is empty but instead the bot crashes.

The API: 应用程序接口

My Code:

const Discord = require("discord.js");
const superagent = require("superagent");

module.exports.run = async (bot, message, args) => {
    let itemid = args.shift().toLowerCase();

    let {body} = await superagent
    .get(`https://us.api.blizzard.com/wow/item/${itemid}?locale=en_US&access_token=TOKEN`);

    let response2 = await superagent.get(`https://us.api.blizzard.com/data/wow/item/${itemid}?namespace=static-us&locale=en_US&access_token=TOKEN`);
    let body2 = response2.body;

    let response3 = await superagent.get(`https://us.api.blizzard.com/data/wow/media/item/${itemid}?namespace=static-us&locale=en_US&access_token=TOKEN`);
    let body3 = response3.body;

    let embed = new Discord.RichEmbed()
    .setColor('RANDOM')
    .setTitle('Item Lookup')
    .setThumbnail(body3.assets[0].value)
    .addField('Item Name:', body.name)
    .addField('Type:', `${body2.inventory_type.name} ${body2.item_subclass.name}`, true)
    .addField('Source:', body.itemSource.sourceType, true)
    .addField('Item ID:', body.id)
    .addField('Display ID:', body.displayInfoId)
    .addField('Item Level:', body.itemLevel)
    .addField('Required Level:', body.requiredLevel)
    .addField(`Effect**(${body.itemSpells[0].trigger})**:`, body.itemSpells[0].scaledDescription) 
    .setFooter(`${body2.quality.name}`);

    if (!body[0].itemSpells) {
        embed .addField(`Effect:`, `This item doesn't have any effects.`);
    }

    message.channel.send(embed);

}

module.exports.help = {
    name: "item"
}

I tried so many things but it just doesn't seem to work for me.

The error I get:

(node:26480) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'trigger' of undefined at Object.module.exports.run (C:\\Users\\ynwa1\\Desktop\\osu-bot\\cmds\\blizzItemlookup.js:27:46) at process._tickCallback (internal/process/next_tick.js:68:7) (node:26480) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:26480) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

It's because you're trying to access 'trigger' on an empty array item here:

body.itemSpells[0].trigger

You can't access .trigger if the item (itemSpells[0]) doesn't exist. Try checking for it first (like you have later in the code):

let embed = new Discord.RichEmbed()
  .setColor('RANDOM')
  .setTitle('Item Lookup')
  .setThumbnail(body3.assets[0].value)
  .addField('Item Name:', body.name)
  .addField(
    'Type:',
    `${body2.inventory_type.name} ${body2.item_subclass.name}`,
    true
  )
  .addField('Source:', body.itemSource.sourceType, true)
  .addField('Item ID:', body.id)
  .addField('Display ID:', body.displayInfoId)
  .addField('Item Level:', body.itemLevel)
  .addField('Required Level:', body.requiredLevel)
  .setFooter(`${body2.quality.name}`)

if (body.itemSpells && body.itemSpells.length) {
  embed.addField(
    `Effect**(${body.itemSpells[0].trigger})**:`,
    body.itemSpells[0].scaledDescription
  )
} else {
  embed.addField(`Effect:`, `This item doesn't have any effects.`)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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