gpt4 book ai didi

angular - ngrx-带有错误处理: fetch data from second api if first api fetch was successful的效果链接

转载 作者:行者123 更新时间:2023-12-03 07:40:13 26 4
gpt4 key购买 nike

我看到了一些SO答案,还有this中级帖子,但是这些仍然不能解决我当前的问题。

场景:

  • 添加产品。
  • 如果成功,则获取所有产品的列表。

  • 这两种情况都可能有错误:
  • 业务错误,包含业务特定的错误消息
  • 网络错误,通用错误(例如40x错误代码等)

  • 目前,我的效果是将使用服务来添加产品,并根据操作失败的成功来调度操作。这将使用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调用以从服务器获取产品列表吗?

    最佳答案

    只需添加另一个@Effect即可触发AddProductSuccess

    @Effect()
    listAllProducts$: Observable<Action> = this.actions$.pipe(
    ofType(AddProductSuccess),
    ....

    关于angular - ngrx-带有错误处理: fetch data from second api if first api fetch was successful的效果链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55041064/

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