如果嵌入没有author.name,它会抛出一个错误



如果读嵌入没有author.name,它将停止工作并发送一个错误

错误C:Userswindows 10 桌面 bot_keytritz index.js: 64If (embed[i].author.name.length>= 0)^

TypeError: Cannot read properties of null (reading 'name')

CODE
if(message.embeds.length >= 0) 
// Check if the Message has embed or not
{
let embed = message.embeds
// console.log(embed) just a console.log
for(let i = 0; i < embed.length; i++)
// Loop it since in v13 you can send multiple embed in single message
{
if(embed[i].author.name === null) return;
// check each embed if it has title or not, if it doesnt then do nothing
{
if(ListClaims.includes(embed[i].author.name.toLowerCase()))
// check each embed if it includes word or not
{
message.react('🎉')
}
}
}
}

那么为什么在访问之前不检查它是否存在呢?有两种方法。

  1. 带有可选链操作符的现代版本

if (embed[i]?.author?.name === null) return;

或旧的

if (!embed[i] || !embed[i].author || embed[i].author.name === null) return;

相关内容