筛选器抛出 - 无法读取 属性 'toLowerCase' of null

Filter throws - Cannot read property 'toLowerCase' of null

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

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

它工作正常,直到 cachedResult 在该字段中没有 NULL。但是如果有NULL我怎么能在这里转义那个记录呢

在使用 toLowerCase() 之前,您必须检查对象中是否存在 属性 prj

result =  this.cachedResults.filter(f => {
  return f.prj ? (f.prj.toLowerCase().indexOf((this.sV).toLowerCase()) !== -1) : false
});

这对你有用吗?

如果问题是 (this.sV) 不存在,那么您也必须检查一下。

您可以执行以下操作来捕获 f.prj 中的空值:

result =  this.cachedResults.filter(f => { 
    if (!f.prj) {
        return false; // Or return true, if you want
    }
    return f.prj.toLowerCase().indexOf((this.sV).toLowerCase()) !== -1);
});

如果 f.prjNULL 那么它不能包含 this.sV 所以特定的 f 应该被过滤掉,对吗?在那种情况下:

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