javascript - discord bot node.js 空数组解析问题
问题描述
这是一个 API,并且有一个带有红色下划线的空数组。有时它是空的,但有时它不是。我正在尝试使结果显示项目法术:对这个项目没有影响。当数组为空但机器人崩溃时。
API:
我的代码:
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"
}
我尝试了很多东西,但它似乎对我不起作用。
我得到的错误:
(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.
解决方案
这是因为您试图在此处访问空数组项上的“触发器”:
body.itemSpells[0].trigger
如果项目 (itemSpells[0]) 不存在,则无法访问 .trigger。首先尝试检查它(就像您在代码中稍后所做的那样):
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.`)
}