我希望我的catch错误返回一个新的LoginFailure操作,但带有来自翻译服务订阅的消息。该实现使我:
类型'(error:any)=> void'的参数不能分配给参数 类型为'(err:any,catch:Observable)=> ObservableInput <{}>'。
@Effect()
login$ = this.actions$.ofType<Login>(AuthActionTypes.Login).pipe(
map(action => action.payload),
exhaustMap(auth =>
this.authService.login(auth).pipe(
map(data => new LoginSuccess({ data: data })),
catchError(error => {
this.translateService
.get('LoginErrorMessage')
.subscribe((res: string) => {
of(new LoginFailure(res));
});
}
)
)
)
);
任何帮助将不胜感激。
答案 0 :(得分:3)
catchError
。mergeMap()
(展平)Observable而不是Subscription。根据您希望此请求的接收者将消息作为成功消息还是错误消息,您必须返回错误消息或成功消息。
@Effect()
login$ = this.actions$.ofType<Login>(AuthActionTypes.Login).pipe(
map(action => action.payload),
exhaustMap(auth =>
this.authService.login(auth).pipe(
map(data => new LoginSuccess({ data: data })),
catchError(error => {
return this.translateService
.get('LoginErrorMessage')
.pipe(
mergeMap((res: string) => {
return of(new LoginFailure(res)); // to get it in success callback
// or return throwError(new LoginFailure(res)) // to get it in error call back
});
)
}
)
)
)
);
如果在收到登录错误后不想调用另一个可观察的对象,则无需使用展平运算符。普通的map()
就可以。
@Effect()
login$ = this.actions$.ofType<Login>(AuthActionTypes.Login).pipe(
map(action => action.payload),
exhaustMap(auth =>
this.authService.login(auth).pipe(
map(data => new LoginSuccess({ data: data })),
catchError(error => {
return this.translateService
.get('LoginErrorMessage')
.pipe(
map((res: string) => {
if (you want the response in success callback) {
return new LoginFailure(res);
}
else {
// if you want it in error
throw "your error"; // say, {error: "I am Error"}
}
});
)
}
)
)
)
);