正确处理ngrx effect catchError
Handle ngrx effect catchError properly
您好,我有一个 Effect,想正确处理错误。我想用服务器端错误的内容调用附加操作。我该怎么做?
@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);
})
)
)
);
不幸的是没有人回答,但无论如何这有效:
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);
}
}
就像你 return 来自 catchError 的 throwError()
结果一样,你应该能够简单地 return 在其他情况下的动作数组
catchError((err) => {
if (foo) {
// whatever you return here, should be an Observable-ish thing
// an Observable of(), an array, etc
return [
new ModalActions.SetModalErrors(err.error),
];
} else {
return throwError(err);
}
})
您好,我有一个 Effect,想正确处理错误。我想用服务器端错误的内容调用附加操作。我该怎么做?
@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);
})
)
)
);
不幸的是没有人回答,但无论如何这有效:
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);
}
}
就像你 return 来自 catchError 的 throwError()
结果一样,你应该能够简单地 return 在其他情况下的动作数组
catchError((err) => {
if (foo) {
// whatever you return here, should be an Observable-ish thing
// an Observable of(), an array, etc
return [
new ModalActions.SetModalErrors(err.error),
];
} else {
return throwError(err);
}
})