I am using angular *ngFor to display a list of users from an endpoint. Now I want to update the picture of each user after rendering the list. But the API to get the picture gets the picture by user Id. So I have to iterate the list and load the image for each user. I am not sure which RXJS operator I can use to achieve this and also how to update the nested users list array.
Get users list data response - Observable<IUsersList> 1
{
"message":"Success",
"data":{
"users":[
{
"id":1,
"firstname":"Bill",
"dateAdded":"2022/02/01",
"picture":"assets/default.png"
},
{
"id":2,
"firstname":"Steve",
"dateAdded":"2022/02/01",
"picture":"assets/default.png"
}
],
"totalPages":1,
"totalElements":2,
"last":true
}
}
Get picture by id response - Observable<IPicture>
{
"id":1,
"picture":"remote.link/bill.png"
}
Here's my attempt, but it's failing. The users observable changes to picture observable when I merge it. So in the subscribe next function there's errors.
this.userService.getUsers(params).pipe(
mergeMap(res => res.data.users.map(user => this.pictureService.getById(user.id)) )
).subscribe({
next: res => {
this.list = res.data.users;
this.isLoading = false;
this.total = res.data.totalElements;
},
error: () => {
this.isLoading = false;
},
})
Kindly help.
this.pictureService.getById(user.id)for all retrieved users simultaneously?