Angular 当映射内有另一个请求时,RxJs处理错误

Angular 当映射内有另一个请求时,RxJs处理错误,angular,rxjs,rxjs5,Angular,Rxjs,Rxjs5,我有用于登录的服务,并按以下方式使用它: // LoginComponent this.authService.loginDb(credentials) .subscribe(() => { this.router.navigate(['/dashboard']); }, (error) => { this.errorMessage = error.de

我有用于登录的服务,并按以下方式使用它:

// LoginComponent
    this.authService.loginDb(credentials)
          .subscribe(() => {
              this.router.navigate(['/dashboard']);
            },
            (error) => {
              this.errorMessage = error.description
            });

//auth.service
public loginDb(credentials: Credentials): Observable <any> {
    //make call to third party API
    return this.auth0.client.login({
            ...
        })
        .map((resp) => {
            // here decode token and then make another request to my server for saving user
            this.token = <Token> this.jwtHelper.decodeToken(resp.idToken);
            return this.userService.createOrUpdateUser(this.token)
                .subscribe(() => {
                    this.localStorageService.set('token', resp.idToken);
                    this.localStorageService.set('decoded_token', this.token);
                    this.loggedIn = true;
                })
        })
}
//LoginComponent
this.authService.loginDb(凭据)
.订阅(()=>{
this.router.navigate(['/dashboard']);
},
(错误)=>{
this.errorMessage=error.description
});
//认证服务
公共登录数据库(凭据:凭据):可观察{
//调用第三方API
返回this.auth0.client.login({
...
})
.map((resp)=>{
//在这里解码令牌,然后向我的服务器发出另一个请求以保存用户
this.token=this.jwtHelper.decodeToken(resp.idToken);
返回this.userService.createOrUpdateUser(this.token)
.订阅(()=>{
this.localStorageService.set('token',resp.idToken);
this.localStorageService.set('decoded_token',this.token);
this.loggedIn=true;
})
})
}
问题是,例如,我的服务器上有错误,并且
this.userService.createOrUpdateUser
失败,那么
错误
将无法在
LoginComponent
中处理此问题。所以,我也希望
error
处理内部请求。此外,我看到在第二个请求完成之前调用了
subscribe

我相信rxjs中有一些操作符可以实现这一点,但我找不到它。

如果您将
map()
替换为
concatMap()
,它将等待它的内部可观察性完成。此外,如果内部可观测对象发出错误,它将沿着链传播

通常,尽量避免嵌套
subscribe()
调用

public loginDb(credentials: Credentials): Observable <any> {
    //make call to third party API
    return this.auth0.client.login({
            ...
        })
        .do(resp => {
            this.localStorageService.set('token', resp.idToken);
            this.localStorageService.set('decoded_token', this.token);
        })
        .concatMap((resp) => {
            // here decode token and then make another request to my server for saving user
            this.token = <Token> this.jwtHelper.decodeToken(resp.idToken);
            return this.userService.createOrUpdateUser(this.token);
        })
        .do(() => {
            this.loggedIn = true;
        });
}
公共登录数据库(凭证:凭证):可观察{ //调用第三方API 返回this.auth0.client.login({ ... }) .do(resp=>{ this.localStorageService.set('token',resp.idToken); this.localStorageService.set('decoded_token',this.token); }) .concatMap((resp)=>{ //在这里解码令牌,然后向我的服务器发出另一个请求以保存用户 this.token=this.jwtHelper.decodeToken(resp.idToken); 返回this.userService.createOrUpdateUser(this.token); }) .do(()=>{ this.loggedIn=true; }); }
A使用了两个
do()
s来执行一些副作用,但可能在您特定的使用案例中,您不需要它们或以不同的方式使用它们。

感谢您的回答do已被tap替换