[英]ngrx - effects chaining with error handling : fetch data from second api if first api fetch was successful
我看到了一些SO答案,以及这个中等帖子,但这些仍然无法解决我当前的问题。
场景:
这两种情况中的每一种都可能有错误:
目前,我具有使用服务添加产品的效果,并根据操作失败的成功发送动作。 这将更新商店,并将currentProduct设置为添加的产品。
@Effect()
addProduct$: Observable<Action> = this.actions$.pipe(
ofType(AddProductAction),
map((action: AddProductAction) => action.payload),
switchMap((ProductToAdd: Product) =>
this.productService.addProduct(ProductToAdd).pipe(
map(response => {
if (this.responseContainsBusinessError(response)) {
return new AddProductBusinessError(response);
} else { // no business error
return new AddProductSuccess(response);
}
}),
catchError(err => of(new AddProductFail(err))) // network/http error etc
)
)
);
有人可以请说明如何更改此代码,以便我可以添加第二个效果/ api调用从服务器获取产品列表?
只需添加另一个将在AddProductSuccess
触发的AddProductSuccess
。
@Effect()
listAllProducts$: Observable<Action> = this.actions$.pipe(
ofType(AddProductSuccess),
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.