【问题标题】:Error TypeError: Cannot read properties of undefined (reading: 'filter')错误类型错误:无法读取未定义的属性(读取:“过滤器”)
【发布时间】:2021-12-31 19:54:30
【问题描述】:
findLoads(){
if(this.loggedInUser.userFullySetupFlag === 0 || this.loggedInUser.businessFullySetupFlag === 0){
  swal(
    'Incomplete Profile',
    'To find loads and bid, all the details inside User Profile (My Profile) and Business Profile (My Business) must be completed.',
    'error'
) 
return;
}

this.shipmentService.filterLoads(this.currentLoadBoardSearch).subscribe(responseObj =>{
  responseObj.searchResults = responseObj.searchResults.filter(function(value, index, arr){return value.loadNumber <= responseObj.loadNumber;});
  this.filteredLoads = responseObj.searchResults;
  console.log("Load Results:", responseObj);
  
});
this.spinner.hide();}

该项目包含许多我无法在此处显示的文件。但请帮我解决这个错误。

【问题讨论】:

  • 看起来您的responseObj.searchResults 未定义。也许响应对象的结构是不同的。尝试将其记录到控制台以查看它的外观。

标签: angular typescript


【解决方案1】:

看起来responseObj.searchResults 是undefined 这就是为什么在对其应用过滤器时抛出错误的原因。 为避免这种情况,只需使用ES2020中引入的safe operator (?)

试试下面的代码:

this.shipmentService.filterLoads(this.currentLoadBoardSearch).subscribe(responseObj =>{
  responseObj.searchResults = responseObj?.searchResults?.filter(function(value, index, arr){return value?.loadNumber <= responseObj?.loadNumber;});
  this.filteredLoads = responseObj?.searchResults;
  console.log("Load Results:", responseObj);
  
});

【讨论】:

  • 它对我有用responseObj.searchResult = (responseObj.searchResult || []).filter(function(value, index, arr){return value.loadNumber &lt;= responseObj.loadNumber;});
猜你喜欢
  • 2018-04-29
  • 2021-12-03
  • 2021-11-22
  • 2019-03-13
  • 1970-01-01
  • 2021-11-05
  • 2022-01-03
  • 1970-01-01
  • 2022-08-13
相关资源
最近更新 更多