sujon
sujon

Reputation: 367

Cannot read property 'includes' of undefined in reactjs?

I try to resolve includes undefined, For that, I am using && operator.

 isAllChecked = label => {
    const { permission } = this.state;

     false value - I need false value when I get data from API,
     But I got an error that includes undefined. for that, I used && operator
     but I got a true value, I don't need to get true value

      const data = !groupItems.some(
      x => !permission && !permission[label] && !permission[label].includes(x)
    );

    // console.log(true) //true

    const data = !groupItems.some(
      x => !permission[label].includes(x)
    );

   // I got a false value using static data without using && operator

  // data output: false (accepted output- getting using static data, but  I need 
   to get the same value when I get data from API, I got an error includes undefined without using && operator)

    return data;

  };

However, If I got data from API, this method is displayed can not read property undefined and when I am going to resolve 'includes' of undefined, I used && operator and I got true value.

 Cannot read property 'includes' of undefined. 

I don't need true value, I need false value for the initially mounted component.

my question is that how can I resolve Cannot read property 'includes' of undefined by using && operator or anything.
Here is my code sandbox: https://codesandbox.io/s/stackoverflow-a-60764570-3982562-v1-um18k

Upvotes: 0

Views: 246

Answers (1)

xdeepakv
xdeepakv

Reputation: 8125

Instead of some, you can use every to check all.

isAllChecked = label => {
    const { permission } = this.state;
    const data = groupItems.every(
      x => permission && permission[label] && permission[label].includes(x)
    );
    return data;
  };

Upvotes: 1

Related Questions