A ShoppingCart with ShoppingCartItems is fetched via an outer REST call, after which an Observable of the ShoppingCartItems makes the inner call to enhance the ShoppingCartItems with a Provider.
A tap(console.log) after the inner call, reveals that the contents of the ShoppingCart are as expected - with the 5 ShoppingCartItems enhanced with a Provider. Tapping the subscription however, returns 5 alerts each containing the Provider I wanted to add as a property of ShoppingCartItem.
It seems I am using the wrong mergeMap/concatMap/switchMap - or not doing a 'collect' of some sort at the end of one or both calls.
The calls:
getShoppingCart$(userId: number): Observable<ShoppingCart> {
return this.rest.getShoppingCart$(userId)
.pipe(
mergeMap(
(shoppingCart) => from(shoppingCart.shoppingCartItems)
.pipe(
concatMap(
item => this.rest.getProviderByWine$(item.wine.id)
.pipe(
map(provider => item.provider = provider),
)
),
// Returns ShoppingCart with Providers added
tap(() => console.log('ShoppingCart: ' + JSON.stringify(shoppingCart)))
)
),
)
}
The subscription:
ngOnInit(): void {
this.shoppingCartService.getShoppingCart$(1037).subscribe(
(shoppingCart: ShoppingCart) => {
this.dataSourceShoppingCart = new NestedMatTableDataSource<ShoppingCartItem>(shoppingCart.shoppingCartItems);
// Runs 5 times - each time displaying a Provider, not the ShoppingCart
alert(JSON.stringify(shoppingCart))
}
);
}
The actual REST calls:
getShoppingCart$(userId: number): Observable<ShoppingCart> {
return this.http.get<ShoppingCart>(this.getBaseUrl() + 'users/' + userId + '/shopping-cart');
}
getProviderByWine$(wineId: number): Observable<any> {
return this.http.get<Provider>(this.getBaseUrl() + 'wine/' + wineId + '/provider');
}
Any pointers are greatly appreciated. Angular version is 8.
switchMapfor that. you can read more about it here:-- medium.com/@luukgruijs/… - user1264429