if the embed does not have author.name it throws me an error

if the read embed does not have author.name, it stops working and sends an error

ERROR C:\Users\windows 10\Desktop\bot_keytritz\index.js:64 if(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('🎉')
        }
        }
      }
    }

Upvotes: 0

Views: 92

Answers (1)

Robert
Robert

Reputation: 20286

Then why you won't check if it exist before accessing? There are 2 ways to do it.

  1. Modern one with Optional Chaining operator

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

or an old one with

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

Upvotes: 1

Related Questions