【问题标题】:Observable combination in NgRx effect - Dispatching an invalid actionNgRx 效果中的可观察组合 - 调度无效动作
【发布时间】:2021-01-01 04:23:50
【问题描述】:

我正在尝试制作一个可以做很多事情并返回成功/失败操作的效果。

这是我正在做的简化版本:

UploadSolidModel$ = this.actions$.pipe(
    ofType<featureActions.UploadSolidRequest>(featureActions.ActionTypes.UploadSolidRequest),
    switchMap((action) =>
      // 1) return an observable with elevation
      this.altimeterService.getElevation()
      // 2) use the above result to make some change and dispatch an API call that return an observable
        .pipe(
          switchMap((elevation) =>
            this.dataService.uploadSolid()
          )
        )
      // 3.a) use the response from the previous api call to dispatch a success/fail effect
        .pipe(
          map((response) => {
            return new ActionSuccess;
          }),
      // 3) 3.b) use the failure of the api call to dispatch failure effect
          catchError((error: HttpErrorResponse) => {
            return of(new featureActions.UploadSolidFailed({ error: error.message }));
          })
        )
    )

我遇到的问题是 NgRx 告诉我我的效果是调度一个无效的动作。 我的链条一定是错的,但我试图修复它,但找不到解决方法。

【问题讨论】:

  • 如果 3a 中的响应来自dataService.uploadSolid,那么您的pipe 级别错误,它应该从uploadSolid 链接起来

标签: javascript rxjs ngrx


【解决方案1】:

您正在返回 3a 处的操作的构造函数。它应该返回一个动作实例。

// notice the `()`
return new ActionSuccess()

【讨论】:

    猜你喜欢
    • 2018-11-22
    • 2019-12-18
    • 2022-01-03
    • 1970-01-01
    • 2020-01-10
    • 2023-03-18
    • 2022-11-16
    • 2017-08-01
    • 2019-01-31
    相关资源
    最近更新 更多