Soham Saha
Soham Saha

Reputation: 57

Cannot read property 'includes' of undefined

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.

bot.on('message', (message) => {
  for (var i = 0; i < message.embeds.length; i++) {
    if (message.embeds[i].title.includes("text!")) {
      message.channel.send('reply')
    }
  }
})

Upvotes: 3

Views: 26833

Answers (7)

Lijo
Lijo

Reputation: 6778

its null pointer error. some of your object parameter is null but its mapped in html. Please try to add more null checks to avoid this .

always check an item before accessing it

bot.on('message', (message) => {
  for (var i = 0; i < message.embeds.length; i++) {
    if (message.embeds[i].title && message.embeds[i].title.includes("text!")) {
      message.channel.send('reply')
    }
  }
})

Upvotes: 0

user8737957
user8737957

Reputation:

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')
      }
    }

  }
});

Upvotes: 0

user11131184
user11131184

Reputation:

It means inside message.embeds[i] there is at least one element without title property.

You should check first if message.embeds[i].title exists and perform other operations after the check.

Upvotes: 1

Shahar Shokrani
Shahar Shokrani

Reputation: 8752

Its because there is at least one item inside the embeds array items that missing the title property.

You will need to update the if statement to be:

If (message.embeds[i] &&
 message.embeds[i].title && ...)

Upvotes: 4

luiscrjr
luiscrjr

Reputation: 7258

Probably some of the embed object is coming without the title property.

You can safely use your logic changing your if condition to:

if ('title' in message.embeds[i] && message.embeds[i].title.includes("text!")) {
   /* ... */
}

Upvotes: 0

Emre Erdoğan
Emre Erdoğan

Reputation: 119

You can write your code more defensive like this. Instead of

if(message.embeds[i].title.includes("text!"))

you can write the following

if(typeof message.embeds[i].title === "string" &&
message.embeds[i].title.includes("text!"))

Upvotes: 0

Moein Alizadeh
Moein Alizadeh

Reputation: 755

I think this code can fix this problem.


bot.on('message', (message) => {
  for (var i = 0; i < message.embeds.length; i++) {
    if (message.embeds[i] && message.embeds[i].title.includes("text!")) {
      message.channel.send('reply')
    }
  }
})

Upvotes: 1

Related Questions