我正在实现ProductCache,但是它会抛出以下错误,
ERROR TypeError:无法读取未定义的属性“map”
代码:
private ProductsCache: any[] = [];
getAllProducts(accountId: string): Observable<any[]> {
return this.ProductsApiService.GetAllProducts(accountId, false).map((Products) => {
this.ProductsCache = Products;
return Products;
});
}
getProductDetailsById(id: number): Observable<any> {
if(this.ProductsCache.length < 1) {
this.getAllProducts(this.AccountApiService.getAccountId()).subscribe((Products: any) => {
let ProductDetails = Products.find(x => x.id === Number(id));
return Observable.of(ProductDetails);
});
} else{
let ProductDetails = this.ProductsCache.find(x => x.id === Number(id));
return Observable.of(ProductDetails);
}
}
我只想通过id获取产品详细信息,如果缓存不存在,那么在返回详细信息之前获取它。使用角4.3.0
发布于 2020-01-29 01:38:24
映射它的管道法
getAllProducts(accountId: string): Observable<any[]> {
return this.ProductsApiService.GetAllProducts(accountId, false)
.pipe(
map((Products) => {
this.ProductsCache = Products;
return Products;
})
)
}
发布于 2020-01-29 01:36:41
this.ProductsApiService.GetAllProducts(accountId, false)
可能是未定义的。
您应该得到响应对象,并且应该与您期望的模式相匹配。
https://stackoverflow.com/questions/59964032
复制相似问题