首页 > 解决方案 > 过滤器抛出 - 无法读取 null 的属性“toLowerCase”

问题描述

我正在尝试根据缓存的查询结果过滤给定的输入。由于我再次过滤用户输入值和数据库,我将它们转换为小写并检查

result =  this.cachedResults.filter(f => f.prj.toLowerCase().indexOf((this.sV).toLowerCase()) !== -1);

它工作正常,直到 cachedResultNULL在该字段中没有。但是,如果有 NULL 我怎么能在这里逃脱该记录。

标签: javascriptangulartypescriptangular7

解决方案


如果f.prj是,NULL那么它不能包含,因此应该过滤掉this.sV特定的内容,对吗?f在这种情况下:

result = this.cachedResults.filter(f => f.prj && f.prj.toLowerCase().indexOf((this.sV).toLowerCase()) !== -1);

推荐阅读