Currently, I am making multiple API requests with different data and storing all the responses in an array. Then, I am using .map to map the response array to my original array to update the data. However, it seems like the process is not working correctly as the response data is showing up as undefined.
In my code:
statusArray = [
{
status: "Submitted",
count: 0
},
{
status: "Approved",
count: 0
},
{
status: "Rejected",
count: 0
},
{
status: "Open",
count: 0
}
];
arr;
temp =[];
status = ["Submitted", "Approved", "Rejected", "Open"];
async sendToApi(){
for(let val of this.status)
{
this.arr = await this.getdata(val);
}
console.log(this.arr);
await this.resolveData(this.arr);
}
async getdata(status)
{
this._service.getStatusBasedCount(status).pipe(takeUntil(this._onDestroy))
.subscribe(async res =>{
if(res['err'] == 'N')
{
this.temp.push({status: res['dataType'], count: res['dataTypeValue']});
return this.temp;
}
});
}
async resolveData(arr)
{
let data = arr;
console.log(data);
this.statusArray= this.statusArray.map((item, row) => {
const found = data.find(
element => item.status == element.status
);
return { ...item, ...found };
});
}
When returning this.temp value, it works fine. But when assigned to arr and printed in console, it returns undefined. Can anyone guide me on how to fix this?