ngrx效果不发射动作

时间:2018-03-22 23:15:02

标签: node.js angular ngrx

我对ngrx有疑问。在效果中,我在NodeJS中调用后端路由,如下:

@Effect() 
authSignup = this.actions$ 
    .ofType(AuthActions.TRY_SIGNUP)
    .switchMap((action: AuthActions.TrySignup) => { 
        return this.httpClient.post('nodejs_backend_route') 
        .map((response: any) => { 
            return Observable.of({ type: AuthActions.SUCCESSFUL_SIGNUP }); 
        })
    });

如果.post内的路由是NodeJS中的后端路由,则无法识别.map内触发的操作。我收到这个错误:

“Error: Effect "AuthEffects.authSignup" dispatched an invalid action: [object Object]”

然而,行动AuthActions.SUCCESSFUL_SIGNUP存在。如果没有调用后端,则操作会完全触发。从ngrx效果中调用NodeJS后端是否存在问题?

谢谢!

1 个答案:

答案 0 :(得分:4)

Try it like this:

@Effect()
authSignup$: Observable<AuthActions.All> = this.actions$
  .pipe(
    ofType<AuthActions.TrySignup>(AuthActions.TRY_SIGNUP),
    switchMap((action) => this.httpClient.post('nodejs_backend_route')
      .pipe(
        map((response) => new AuthActions.SuccessfulSignup(response)),
        catchError((error) => of(new AuthActions.FailedSignup(error)))
      )
    )
  );

Problem is you are not returning a Action in your map. I have rewrote it to give you a hint how it could be done with the latest versions of vendors.