Search code examples
angulartypescriptangular2-filtering

Typescript: Cannot read property 'toLowerCase' of undefined


I am getting this error on line:

return filter ? value.filter(element => element.type.toLowerCase().indexOf(filter.toString().toLowerCase()) != -1) : value;

when element.type is an empty string (filtering empty column). How to fix this?


Solution

  • As the message states: element.type is undefined. Add a check on that in your filter call. Something like this, what you put depends on what you are expecting back and I can only guess at that.

    return filter 
        ? value.filter(element => element.type && element.type.toLowerCase().indexOf(filter.toString().toLowerCase()) != -1) 
        : value;