简体   繁体   English

如何在catcherror中从订阅中返回已翻译的消息?

[英]How do I return a translated message from a subscription in a catcherror?

I want my catch error to return a new LoginFailure action but with a message that comes from the subscription of the translate service. 我希望我的catch错误返回一个新的LoginFailure操作,但是带有来自翻译服务订阅的消息。 This implementation is giving me: 这个实现给了我:

Argument of type '(error: any) => void' is not assignable to parameter of type '(err: any, caught: Observable) => ObservableInput<{}>'. 类型'(error:any)=> void'的参数不能赋给类型'(err:any,caught: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));
          });
        }
        )
      )
    )
  );

any help would be appreciated. 任何帮助,将不胜感激。

  1. You get that error because you aren't returning anything from catchError . 您收到该错误,因为您没有从catchError返回任何catchError
  2. You got to return an Observable not a subscription, so use mergeMap() (to flatten) the Observable instead of Subscription. 你必须返回一个Observable而不是订阅,所以使用mergeMap() (展平)Observable而不是Subscription。
  3. Depending you want the receiver of this request to get the message as a success or an error you have got to return an error or a success. 根据您希望此请求的接收者将消息视为成功或错误,您必须返回错误或成功。

      @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 }); ) } ) ) ) ); 


Update 更新

In case you don't want to call another observable after you get a Login Error, there is no need of using a flattening operator. 如果您在获得登录错误后不想调用另一个observable,则无需使用展平运算符。 A normal map() will do. 法线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"} } }); ) } ) ) ) ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM