2
我正在使用Angular 2最終版本並嘗試執行依賴於另一個HTTP請求的HTTP請求。Angular2:Observable flatMap error無法讀取未定義的屬性'subscribe'
我的情況下試圖getAccount與用戶id作爲參數,它從另一個HTTP請求返回(的getUser)
帳戶服務:
constructor(private http: Http, private userService: UserService) {
}
getAccount(): Observable<Account> {
let tenant: string = 'test';
Observable.fromPromise(this.userService.getUser()).flatMap(
user => {
return this.http.get('/tenants/' + tenant + '/accounts/' + user.id + '/')
.map((res: Response) => {
return res.json();
});
});
}
UserService(取決於外部系統上,工作原理要求):
getUser(): Promise<User> {
return new Promise<User>((resolve, reject) => {
if (KeycloakService.auth.authz.token) {
return KeycloakService.auth.authz.loadUserInfo().success((data) => {
resolve(<User> data);
}).error(() => {
reject(null);
});
}
});
}
當調用getAccount從AppComponent.ts:
this.accountService.getAccount().subscribe(data => { console.log('account:', data); });
,我發現了以下錯誤:
EXCEPTION: Uncaught (in promise): Error: Error in :0:0 caused by: Cannot read property 'subscribe' of undefined TypeError: Cannot read property 'subscribe' of undefined
任何想法是什麼原因導致這個錯誤?
感謝
感謝!我不敢相信這麼多時間在這 –
不客氣。很高興聽到這個問題解決了你的問題:-) –