如果出现http错误,如何保证可观察流的连续性?

时间:2018-02-02 06:06:48

标签: angular observable rxjs5 ngrx ngrx-effects

当遇到onTrySignin错误响应时,我遇到以下方法(HTTP)的问题。 catch电话后HTTP阻止Side Effect阻止Action出现console.log错误。如果我Observable我收到此错误。

  

TypeError:您提供了' undefined'预期流的地方。您可以提供Observable,Promise,Array或Iterable。

如何保留mergeMap流并将响应传递到下一个块(Actions),在这种情况下我可以触发其他FailedSignin(),(onTrySignin = this.actions$ .ofType(AuthActions.TRY_SIGNIN) .map((action: AuthActions.TrySignin) => { return action.payload; }) .switchMap((action: DispatchAction) => { const trySignInPayload: TrySignInPayload = action.payload; return this.httpService.postRequest('Account/Login', (trySignInPayload.loginData)) .catch((error: any) => { console.log(error) return Observable.empty(); }) .mergeMap((response: HttpResponse<any>) => { switch (response.status) { case 200: if (trySignInPayload.returnUrl) { this.router.navigate([trySignInPayload.returnUrl]); } else { this.router.navigate(['/dbapp']); } return Observable.concat( Observable.of(new AuthActions.GenerateAntiforgeryToken()), Observable.of(new AuthActions.Signin(fromAuth.authId, this.fetchUserData())) ); case 401: case 404: return Observable.concat( Observable.of(new AuthActions.FailedSignin()), Observable.empty() ); default: return Observable.concat( Observable.of(new AuthActions.FailedSignin()), Observable.empty() ); } }) }).catch((error) => { return Observable.throw(error); }); ) ?

httpService

这是我的public postRequest(apiUrl: string, jsonData: {} = {}): Observable<any> { return this.httpService.post(this.baseUrl + apiUrl, JSON.stringify(jsonData), {observe: 'response', reportProgress: true, withCredentials: true}); }

   filter_vars <- c("source", "medium", "campaign", "userType", "deviceCategory", "Condition",
                    "Voice", "Format", "Type", "Stage_Topic"),

1 个答案:

答案 0 :(得分:1)

You need to create a disposable stream, this is how we do it:

@Effect()
  login$: Observable<Action> = this.actions$
   .ofType(authActions.LOGIN)
   .switchMap((action: any) => {
     // @Effect stream is completing when the error occurs, preventing any further 
     // actions. Therefore create a disposable stream to keep @Effect stream alive
     return Observable.of(action)
       .switchMap((action: any) => {
         return this.apiService.login(action.payload);
       })
       .map((x: any) => {
         return new authActions.SetTokensAction({ token: x.data.token });
       })
       .catch((error: any) => {
         return Observable.of(new authActions.LoginFailedAction(error));
       });
    });