Typescript: Cannot read property 'toLowerCase' of undefined

梦想与她 提交于 2020-08-11 08:44:12

问题


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?


回答1:


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;



回答2:


This error itself gives the solution as the value passed to change into lowercase is null or undefined. To solve such errors, put the statements for the data to be passed in an 'if' statement.

Like :

if(somevalue != null || somevalue != undefined)
{ ... }

Putting the statements inside if statement solved my issue.




回答3:


Well said, error itself gives the solution as the value passed to change into lowercase is null or undefined.

Just add to your line of code, where you see error coming

if(somevalue != null || somevalue != undefined)
{ ... }


来源:https://stackoverflow.com/questions/42542137/typescript-cannot-read-property-tolowercase-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!