Home > Blockchain >  Reactjs not recognize some of the ES6 function?
Reactjs not recognize some of the ES6 function?

Time:12-14

I am new in Reactjs, I tried to use ES6 code in my React js file like the following, the compiler not recognized filter and includes?? Error : Uncaught TypeError: Cannot read properties of undefined(reading 'filter')...

const noStoreName = csvFile.filter( e => categoriesFile.includes(e))

Thanks!

CodePudding user response:

It seems that it's not from compiler

cvsFile is probebly undefiend

so simply add question mark or set if condition

const noStoreName = csvFile?.filter( e => categoriesFile.includes(e))

or :

if(csvFile){
  const noStoreName = csvFile.filter( e => categoriesFile.includes(e))
}

CodePudding user response:

This is not ES6 issue, it is happening because of your csvFile array is empty or undefined in initial, you can do like this:

const noStoreName = csvFile && csvFile.length > 0 && csvFile.filter(e => categoriesFile.includes(e))

If you want to use optional chaining then like this:

const noStoreName = csvFile?.filter( e => categoriesFile?.includes(e))
  • Related