gpt4 book ai didi

angular - 将 http 响应映射到 Angular 7 中的接口(interface)

转载 作者:可可西里 更新时间:2023-11-01 16:58:56 25 4
gpt4 key购买 nike

我有 Angular 购物模板,我正在尝试连接用于我的后端的购物车服务,所以我已经这样做了,但是为了让数据库中的事情变得更容易,我不得不将请求从 更改为我的后端{quantity, amount, product}{quantity, amount, productId} 所以我不必将整个产品存储在数据库中并且到目前为止它工作正常,现在当我得到响应我想将它映射到它的界面,但首先我需要找到具有我在响应中的 id 的产品,如果我有整个产品,我可以映射响应但我又不想将产品保存在我的数据库中。这是我的映射代码,但我在调用通过 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;
}

最佳答案

您可以采用以下方法,请注意我创建了一些使用假数据的工厂方法 以便对其进行测试。您可以将它们替换为您的实际实现:

import { of, Observable } from 'rxjs';
import { map, mergeMap, toArray } from 'rxjs/operators';

export interface Product {
id: string;
name: string;
}

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;
}

const fakeGetAllProducts = (): Observable<CartResponse[]> => of<CartResponse[]>([
{ currency: "US", product: "PX1", quantity: 10, selectedColor: "red", selectedSize: "L", userId: "UX1" },
{ currency: "EU", product: "PX50", quantity: 10, selectedColor: "blue", selectedSize: "S", userId: "UX2" }
]);
const fakeGetProduct = (id: string): Observable<Product> => of<Product>({ id, name: `Product ${id}` });

// Here the cart response is destructured into 2 values: the product id and everything else
const mapResponseToItem = ({ product, ...noProduct }: CartResponse): Observable<CartItem> => fakeGetProduct(product).pipe(
map<Product, CartItem>(product => ({ ...noProduct, product }))
);

fakeGetAllProducts().pipe(
// flatten the array in order to process single items from it sequentially
mergeMap(items => items),
// map a cart response into a cart item observable and flatten it
mergeMap(mapResponseToItem),
// collect the sequentially processed elements into an array
toArray()
).subscribe(console.log);

您可以在 this blitz 中看到有效的代码

关于angular - 将 http 响应映射到 Angular 7 中的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57014771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com