Node.js 角度5错误类型错误:无法读取属性';toLowerCase';未定义的
我在chrome的控制台上遇到了以下错误: 错误类型错误:无法读取属性 未定义的“toLowerCase” 在DashboardComponent上。html:5:Node.js 角度5错误类型错误:无法读取属性';toLowerCase';未定义的,node.js,angular,mean,Node.js,Angular,Mean,我在chrome的控制台上遇到了以下错误: 错误类型错误:无法读取属性 未定义的“toLowerCase” 在DashboardComponent上。html:5: <div class="mdl-grid center-items"> <div class="mdl-cell mdl-cell--4-col"> <mat-form-field class="search-form-field"> <input mat
<div class="mdl-grid center-items">
<div class="mdl-cell mdl-cell--4-col">
<mat-form-field class="search-form-field">
<input matInput type="text" name="searchCompany" placeholder="Search" [(ngModel)]="searchCompany"/>
<button *ngIf="searchCompany" matSuffix mat-icon-button aria-label="Clear" (click)="searchCompany=''">
<mat-icon>close</mat-icon>
</button>
</mat-form-field>
</div>
</div>
我已经在app.module.ts上声明了CompanyFilterPipe,但它不工作,我如何修复它?谢谢。看起来
perusahaan
属性在公司
对象上未定义。尝试在公司
类型中声明,或在函数声明中使用公司:任意[]
。此外,还应在筛选器内使用不同的变量名
编辑
1) 使用searchcompanys:string
代替string
2) !公司==!searchCompany
将始终为true,除非其中任何一个字段为空,否则不会到达您的下返回语句
3) 使用companys.filter(companys=>..
将发生冲突,因为您为数组和筛选器组件指定了相同的名称。请像这样使用:source.pipe(filter(num=>num>5))
4) 检查searchCompany是否为空。管道声明中并没有错误。管道正在被触发,这就是为什么您会收到与写入管道中的逻辑相关的错误 toLowerCase()方法应仅应用于字符串 由于错误表示无法读取undefined的属性“toLowerCase”,因此需要验证发送到管道的输入。toLowerCase()应用于两个属性“searchCompany”和“Companys.perusahaan”,请在管道中放置调试器,并验证这两个属性的值都是字符串。通过添加以下内容解决:
if(!searchCompany){
return companies;
}
因此,当搜索输入为空时,我返回公司,但当输入有值时,它将返回
return companies.filter(companies => companies.perusahaan.toLowerCase().indexOf(value) !== -1);
感谢您帮助我善良的人们:)但是您没有在模板中的任何地方使用
companyFilter
?我只在这个页面中使用。使用if来考虑searchCompany不是空的:if(searchCompany){…}else{return[]}//返回一个空数组我已经定义了它,我使用它来为公司命名。尝试使用any[]声明或console.log companys.perusahaan以查看实际传递的内容。我创建了导出类Company{perusahaan:String;},但在检查Company.perusahaan时,它返回undefined。为什么会发生这种情况?但是我console.log(companys)它在数组内的对象中返回我的数据;但是为什么呢?当我删除返回公司时,过滤器正在工作,但我的控制台上仍然会出现错误。当我检查console.log中的值时,它将返回我在输入中键入的输入。你是指searchCompany的值?是,searchCompany的值。
return companies.filter(companies => companies.perusahaan.toLowerCase().indexOf(value) !== -1);