Alex Verzea
Alex Verzea

Reputation: 441

ngrx effect not firing action

I have an issue with ngrx. Inside an Effect, I call a Backend route in NodeJS, as such:

@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 }); 
        })
    });

If the route inside .post is a Backend route in NodeJS, the action fired inside .map is not recognized. I get this error:

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

Yet the action AuthActions.SUCCESSFUL_SIGNUP exists. If no call to the Backend is made, the action fires perfectly. Is there an issue with making calls to a NodeJS Backend from inside an ngrx Effect?

Thanks!

Upvotes: 2

Views: 3382

Answers (1)

Kamil
Kamil

Reputation: 1641

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.

Upvotes: 4

Related Questions