Reputation: 71
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!
Upvotes: 0
Views: 29
Reputation: 3559
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))
}
Upvotes: 2
Reputation: 1187
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))
Upvotes: 1