Mike Micky
Mike Micky

Reputation: 21

Cannot read properties of undefined (reading 'forEach') error in angular

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.

Upvotes: 0

Views: 1128

Answers (2)

Ediz Suleyman
Ediz Suleyman

Reputation: 44

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

Upvotes: 0

MGX
MGX

Reputation: 3521

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.

Upvotes: 1

Related Questions