playerone playerone -4 years ago 304
TypeScript Question

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 empty string (filtering empty column). How to fix this?

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download