Utkarsh
Utkarsh

Reputation: 1

Error TypeError: Cannot read properties of undefined (reading: 'filter')

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();}

The project consist many files which I am unable to show here. But kindly help me to resolve this error.

Upvotes: 0

Views: 1279

Answers (1)

Suneet Bansal
Suneet Bansal

Reputation: 2702

Looks like responseObj.searchResults is undefined that's why throwing error when apply filter on it. To avoid this, just use safe operator (?) introduced in ES2020

Try below code:

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);
  
});

Upvotes: 1

Related Questions