2017-07-12 7 views
-2

以下のコード内のhttpコールがthis.api.one( 'profiles'、ui.sub.get()は実行されていません。私は内部マップ呼び出しがObservableを返し、flatMapでラップされるので、最後にsubscribe()を呼び出す必要があるだけであると誤解しているようです。RxJs - 操作を連鎖させる方法

this.$http.get(uiUri, {headers: new Headers({'Authorization': 'Bearer ' + user.access_token})}) 
    .map(resp => resp.json()) 
    .flatMap((ui: IID4UserInfo) => { 
     let partialAccount = new Account(ui); 
     partialAccount.isLoggedIn = false; 
     this.currentAccount = partialAccount; 
     return this.api.one('profiles', ui.sub).get().map((profile) => { 
      this.currentAccount = new Account(profile.plain()); //never executes 
      this.storage.set('accountService.localAccount', this.currentAccount); 
      this.currentAccount.isLoggedIn = true; 
      this.accountSource.next(this.currentAccount); 
      if (this.loader) 
       this.loader.dismiss(); 
      return this.currentAccount; 
     }, (err) => { 
      this.log.error(err); 
      this.currentAccount = partialAccount; 
      this.accountSource.next(partialAccount); 
      if (this.loader) 
       this.loader.dismiss(); 
      return this.currentAccount; 
     }) 
    }) 
    .subscribe((profile) => { 
     this.log.info('user fully loaded'); 
    }, err => { 
     this.log.error(err) 
    }); 

FYI this.api.one( 'プロフィール'、ui.sub)に.get()がに解決:

get(queryParams?: string, headers?: any): Observable<IElement> { 
    let path = queryParams ? this.currentPath + '?' + queryParams : this.currentPath; 
    return this.api.http.get(path, {headers: this['_getHeaders'](headers)}).map(r => r.json()).map(resp => { 
     return Object.assign(new Element(this.api, this.currentPath), resp); 
    }); 
} 

答えて

0

私はあなたがエラーコールバックに渡すことができるとは思いません.map()機能で代わりに.catch()を使用してください。

this.$http.get(uiUri, {headers: new Headers({'Authorization': 'Bearer ' + user.access_token})}) 
    .map(resp => resp.json()) 
    .flatMap((ui: IID4UserInfo) => { 
     let partialAccount = new Account(ui); 
     partialAccount.isLoggedIn = false; 
     this.currentAccount = partialAccount; 
     return this.api.one('profiles', ui.sub) 
      .get() 
      .map((profile) => { 
       this.currentAccount = new Account(profile.plain()); //never executes 
       this.storage.set('accountService.localAccount', this.currentAccount); 
       this.currentAccount.isLoggedIn = true; 
       this.accountSource.next(this.currentAccount); 
       if (this.loader) 
        this.loader.dismiss(); 
       return this.currentAccount; 
      }) 
      //use a catch block 
      .catch((err) => { 
       this.log.error(err); 
       this.currentAccount = partialAccount; 
       this.accountSource.next(partialAccount); 
       if (this.loader) 
        this.loader.dismiss(); 
       return this.currentAccount; 
      }) 
    }) 
    .subscribe((profile) => { 
     this.log.info('user fully loaded'); 
    }, err => { 
     this.log.error(err) 
    }); 
+0

ありがとうございます。コードを更新しましたが、残念ながら問題は解決しませんでした。 httpコールはまだ出ていません。 –

+0

@Justinは 'profile.plain()'でも非同期呼び出しですか? – CozyAzure

+0

それは今働いています。 GETリクエストが送信される前にOPTIONSリクエストがタイムアウトしていましたが、プロキシの下で再度実行するまではキャッチしませんでした –

関連する問題