首页 > 解决方案 > 错误类型错误:无法读取未定义 core.js:5967 的属性“toLocaleLowerCase”

问题描述

组件.ts

branches: any = [];
branchName: string; // any
ngOnInit(): void {
    // this.refreshBranchList();
    this.service.getBranchList().subscribe((response: any) => {
      this.branches = response;
});
}
Search() {
if (this.branchName !== "") {
  this.branches = this.branches.filter((res: { branchName: string; }) => {
    return res.branchName.toLocaleLowerCase().match(this.branchName.toLocaleLowerCase());
  })
} else if (this.branchName == "") {
  this.ngOnInit();
}

}

组件.html

<div class="d-flex float-right">
  <input type="text" class="form-control" placeholder="Search here" [(ngModel)]="branchName" (ngModelChange)="Search()">
</div>

它正在按预期搜索值。但是控制台中有一个我不熟悉的错误。有什么我想念的请告诉我。

错误详情

标签: angulartypescriptdjango-rest-framework

解决方案


尝试改变这一点

return res.branchName.toLocaleLowerCase().match(this.branchName.toLocaleLowerCase());

对此

return res.branchName?.toLocaleLowerCase().match(this.branchName?.toLocaleLowerCase());

推荐阅读