'Getting can not read the property of undefined error while implementing for each loop in angular

I am trying to loop two different object like the following code example,

this.selectedRoomListObj.data.forEach(element => {
            
            this.dataSourceBlockCode.data.forEach(item => 
              {
                this.nPercentageAssigned.push(item.nPercent);
                this.nccpId.push(item.nClientCPCMappingId);
              });
          var weightedAverageRoomObj={
            nPercentageAssigned: this.nPercentageAssigned,
            nccpId: this.nccpId,
            nRoomAllocationId:element.nRoomAllocationId
          };
          console.log(this.weightedAverageRoomObj);
          this.blockcodeService.addWeightAverageRoomApiMethod(this.weightedAverageRoomObj)
          .subscribe(data=> {console.log(data)});

        });

And getting error like the following an it points to the line where I am looping selectedRoomListObject- " this.selectedRoomListObj.data.forEach(element => {" this line.

enter image description here I tried to troubleshoot by putting console.log for selectedRoomListObj object. And its successfully printing the object data.

My Front-end code for triggering event is as below,

<ng-container  style="max-width:40px; ">
                <input #input type="number" [(ngModel)]="element.nPercent" 
(change)="calculateTotalAndAddRecord()" (keyup.enter)="calculateTotalAndAddRecord()" >%
 </ng-container>

Troubleshoot Try2

When I tried to console.log(this), I got the console printing like the following,

enter image description here

Here I am getting selectedRoomListObj , but not showing dataBlockCode object. When I tried to print this.dataBlockCode using console.log , at that time i am getting objects. But not for this. And the selectedRoomListObj is already assigning using another function onclicking another button. this 2 functions are calling serially one after another.

The other function definition is ,

  assignSelectedRoomRecordsToObject(){
    
    this.selectedRoomListObj =this.checkedDataSource.data;
    
   }

Can anyone guide to troubleshoot this problem ? Am I doing wrong syntax in looping ? Or Am I following wrong inner loops?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source