Home > Back-end >  Cannot read properties of undefined (reading 'forEach') error in angular
Cannot read properties of undefined (reading 'forEach') error in angular

Time:10-14

This is my model and im trying to loop to get some required data and i face the above error.

export class HubxDataModel{ 
    categoryId:number;
    categoryName:string;
    HubxDataItems:[HubxModel];
}
export class HubxModel{ 
    id: number;
    categoryId: number;
    itemTitle: string;
    itemUnit: string;
    isActive: boolean=true;
    itemValue: string;
    normalRange:string;
    itemColor : string;
    patientId: number;
    isDeleted: boolean;
}

Code that i tried to loop .Error comes at b.hubxdataitems line of code.From API i got the data for hubxdataitems array.

 let hubxdata: Array<any>=[];
    let hubxitem:Array<any>=[];
    this.hubxReportList.forEach((b)=>{
      hubxdata.push(b.categoryName)
      b.HubxDataItems.forEach((c)=>{  <---error comes here--->
        hubxitem.push(c.itemTitle,c.itemValue,c.itemUnit,c.normalRange)
      })

    })

how can i fix this issue.

CodePudding user response:

The answer has data in it. So this should be it :

b.data[0].HubxDataItems.forEach(...)

Take the time to also push b.data.categoryName too.

CodePudding user response:

Can you please share more information about your issue you have:

  • What is your error you get?
  • The declaration of hubxReportList.
  • When you console log the "b" here: this.hubxReportList.forEach((b) do you get any or not?

One thing i realised that from api you get camelcase hubxDataItems whereas in foreach loop you try to loop a pascal case property: b.HubxDataItems.forEach

  • Related