I have angular shopping template and I am trying to connect the cart service that was used to my backend, so I have done so but to make thing easier in the database I had to change the request to my backend from {quantity, amount, product} to {quantity, amount, productId} so I don't have to store the whole product in the database and its working fine so far, now when I got the response I want to map it to its interface but first I need to find the product with the id that I have in the response, I am able to map the response if I have the whole product but again I don't want to have the product saved in my database. here is my code for the mapping but I have an issue calling my function that find the product by id:
this.getAllProducts().pipe(map(p => p.map((s): CartItem => ({
currency: s.currency,
product: this.productService.getProduct(s.product).subscribe(p => p),
quantity: s.quantity,
selectedColor: s.selectedColor,
selectedSize: s.selectedSize,
userId: s.userId
}))));
private getAllProducts(): Observable<CartResponse[]> {
return this.http.get<CartResponse[]>('http://localhost:8082/cart/getAllItems?userId=111').pipe(
map(o => o.map((sp): CartResponse => ({
quantity: sp.quantity,
currency: sp.currency,
product: sp.product,
selectedColor: sp.selectedColor,
selectedSize: sp.selectedSize,
userId: sp.userId
}))));
}
export interface CartResponse {
product: string;
quantity: number;
selectedSize: any;
selectedColor: string;
currency: string;
userId: string;
}
export interface CartItem {
product: Product;
quantity: number;
selectedSize: any;
selectedColor: string;
currency: string;
userId: string;
}
mapsection in thegetAllProductsmethod is not necessary. Remove it.pin the nestedmapcall asCarItem? Shouldnt the type ofsbyCartResponse?CartResponse.