私はRXJSでより良い処理をしようとしていますが、私はAudienceService.addAudience()を呼び出すとCannot read property 'map' of undefined
を取得しています。マップされた関数の後のAngular2 rxjs map()はサブスクライブハンドルを完了する
AuthHttpService.post()
のサブスクリプションが完了する前に、AudienceService
がAuthHttpService.post()
にマップしようとすると、トークンのリフレッシュが必要な場合があります。
これをコード化するにはどうすればいいですか?AuthHttpService
が完了した後でmap()
が実行されるようにするにはどうすればよいですか? AuthHttpService.postにマップ
AudienceService:POSTメソッドで
addAudience(model: AddDeleteAudienceModel) {
return this.authHttpService.post(this.baseApiUrl + 'audience', JSON.stringify(model)).map(res => res.json());
}
AuthHttpService:
post(endpoint: string, body: string) {
if (this.authService.tokenRequiresRefresh()) {
this.authService.tokenIsBeingRefreshed.next(true);
this.authService.refreshToken().subscribe(
data => {
this.authService.refreshTokenSuccessHandler(data);
// The audience service post call returns "Cannot read property 'map' of undefined"
console.log("User is now logged in");
if (this.authService.loggedIn()) {
this.authService.tokenIsBeingRefreshed.next(false);
return this.postInternal(endpoint, body);
} else {
this.authService.tokenIsBeingRefreshed.next(false);
this.router.navigate(['/sessiontimeout']);
}
},
error => {
this.authService.refreshTokenErrorHandler(error);
Observable.throw(error);
}
);
}
else {
console.log("HTTP Post token refresh not required.");
return this.postInternal(endpoint, body);
}
}
これを 'RxJs'からインポートしましたか? –
@YashveerSinghはい、マップ自体がインポートされ、トークンのリフレッシュが必要な場合にのみ問題が発生します。それ以外の場合はマップが定義されます。 – blgrnboy
authHttpServiceとは何ですか? –