Angular 正确处理ngrx效应捕捉错误
你好,我有一个效果,希望正确处理错误。我想用服务器端错误的内容调用其他操作。我怎么能这样做Angular 正确处理ngrx效应捕捉错误,angular,ngrx,Angular,Ngrx,你好,我有一个效果,希望正确处理错误。我想用服务器端错误的内容调用其他操作。我怎么能这样做 @Injectable() export class AuthEffects { @Effect() registerUser$ = this.actions$.pipe( ofType(AuthActions.Action.CREATE_USER), switchMap(({user}) => this.customersService.cr
@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);
})
)
)
);
@Injectable()
导出类AuthEffects{
@效果()
registerUser$=此.actions$.pipe(
类型(authoctions.Action.CREATE\u USER),
开关映射({user})=>
这个.customersService.createNewUser(用户).pipe(
开关映射(()=>[
新建ModalActions.SetModalType(null)
]
),
catchError((err)=>{
如果(foo){
新的ModalActions.SetModalErrors(err.error);//不幸的是没有人应答,但无论如何,这是可行的:
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);
}
}
您是否尝试过返回(新ModalActions.SetModalErrors(…)
?@AndreiGătej当然可以。但我需要检查它是否是一个错误。foo作为响应,如果是,我需要添加通知或添加另一个操作。返回throwError(err);
@AlexOkrushko谢谢。返回空[]是否更好?它必须返回一个动作(带或不带有效载荷)或空
可观察。只需注意:将“return SprowerRor()”与您的(可能使用)匹配即可不调用自定义错误处理程序,对吗?我只会调用if语句中的下一个操作,该语句可以命名为“authoctions.action.CREATE_USER_FAILED”(不要忘记在此处使用“dispatch:false”)。然后,在失败操作中,您可以执行任何您想要的操作,而不必以操作循环结束。