正确处理ngrx效果catchError

时间:2020-06-10 11:29:40

标签: angular ngrx

你好,我有一个效果,想正确处理错误。我想调用包含服务器端错误内容的其他操作。我该怎么办?

@Injectable()
export class AuthEffects {
    @Effect()
    registerUser$ = this.actions$.pipe(
      ofType(AuthActions.Action.CREATE_USER),
      switchMap(({user}) =>
        this.customersService.createNewUser(user).pipe(
          switchMap(() => [
            new ModalActions.SetModalType(null)
            ]
          ),
            catchError((err) => {
              if (foo) {
              new ModalActions.SetModalErrors(err.error); // <---- make an Action here
              } else {
              }
              return throwError(err);
            })

            )
        )
    );

1 个答案:

答案 0 :(得分:1)

不幸的是,没有人回答,但是无论如何这是可行的:

catchError((err) => {
  if (err.error.email) {
    return of(new ModalActions.SetModalErrors(err.error));
  } else {
  this.notificationService.notify(Object.values(err.error)[0] as string);
  return throwError(err);
  }
}
相关问题