Cannot read property 'includes' of undefined

前端 未结 6 1383
抹茶落季
抹茶落季 2021-01-21 16:09

I am new to JavaScript, am I was trying to dissect an embedded message. Here\'s my code, it runs fine for a few mins, works accordingly but idk what goes wrong.



        
6条回答
  •  青春惊慌失措
    2021-01-21 16:24

    JavaScript is not a type safe language, and the error is caused by not being type safe. We will have to check if object exists and nested properties exists and after we should be able check the value. In your case:

    
    bot.on('message', (message) => {
    
      // check if main obj and main property exist
      if (message && message.embeds) {
    
        for (var i = 0; i < message.embeds.length; i++) {
          // now, check if title exists and after check the text inside
          if (
            message.embeds[i].title && 
            message.embeds[i].title.includes("text!")) 
          {
            message.channel.send('reply')
          }
        }
    
      }
    });
    
    

提交回复
热议问题