Search code examples
angulartypescriptngrxngrx-storengrx-effects

Effect "AuthEffects.register$" dispatched an invalid action


i've added new effect for register action and got this error: Effect "AuthEffects.register$" dispatched an invalid action

So, this is my effect:

@Effect()
register$ = this.actions$.pipe(
    ofType<Register>(AuthActionTypes.Register),
    map(action => action.payload),
    exhaustMap((register: RegisterModel) =>
      this.authService
        .register(register)
        .pipe(
          tap(status => new RegisterSuccess(status)),
          catchError(error => of(new RegisterFailure(error)))
        )
    )
);

this is RegisterSuccess action:

export class RegisterSuccess implements Action {
  readonly type = AuthActionTypes.RegisterSuccess;

  constructor(public payload: boolean) {}
}

it returns an boolean which says if register was successfully. This is reducer case:

case AuthActionTypes.RegisterSuccess: {
        return {
            ...state,
            error: null,
            success: action.payload
        }
    }

Where is the problem? Effect looks like to be ok, action also, is the problem in my reducer? Thanks!


Solution

  • Your tap should be a map in order to return an action.

    @Effect()
    register$ = this.actions$.pipe(
        ofType<Register>(AuthActionTypes.Register),
        map(action => action.payload),
        exhaustMap((register: RegisterModel) =>
          this.authService
            .register(register)
            .pipe(
              map(status => new RegisterSuccess(status)),
              catchError(error => of(new RegisterFailure(error)))
            )
        )
    );