Javascript 调用返回可观察对象而不是实际对象
我对this.dashboardFacade.getCurrentUseruid.thenuser=>console.loguser的调用;不断返回一个可观察的对象而不是实际的数据对象-有人知道如何修复它吗 仪表板组件 仪表板正面 引发以下错误:Javascript 调用返回可观察对象而不是实际对象,javascript,angular,typescript,observable,subscription,Javascript,Angular,Typescript,Observable,Subscription,我对this.dashboardFacade.getCurrentUseruid.thenuser=>console.loguser的调用;不断返回一个可观察的对象而不是实际的数据对象-有人知道如何修复它吗 仪表板组件 仪表板正面 引发以下错误: promise中未捕获的:TypeError:this.dashboardFacade.getCurrentUser…subscribe不是函数TypeError:this.dashboardFacade.getCurrentUser…subscrib
promise中未捕获的:TypeError:this.dashboardFacade.getCurrentUser…subscribe不是函数TypeError:this.dashboardFacade.getCurrentUser…subscribe不是仪表板组件中的函数。它发生在我承诺返回一个承诺时,中间承诺有点看不见,你会收到最后一个承诺链的结果 您的方法getUser返回一个承诺,但这个承诺实际上不返回另一个承诺,而是一个可观察的承诺,在这种情况下,不涉及链承诺,您将收到一个可观察的承诺。如果您不需要观察,您可以做些什么来转换您的方法以实际返回承诺: getUseruid:string:Promise{ 返回火基 作者 .currentUser.getIdToken .thentoken:string=>{ debug`Token generated:${Token}`; 返回此.httpClient .get`${environment.apiBasePath}/users`{ 标头:{授权:`Bearer${token}`, }.toPromise;console.loguser; }; } };
当您进行HTTP调用时,它会返回一个可观察的值。您可以尝试使用可观察而不是承诺
this.dashboardFacade.getCurrentUserUid().then((uid) => {
if (uid) {
this.dashboardFacade.getCurrentUser(uid).then((user) => console.log(user));
}
});
getCurrentUser(uid: string): Promise<unknown> {
return this.userService.getUser(uid);
}
getUser(uid: string): Promise<unknown> {
return firebase
.auth()
.currentUser.getIdToken()
.then((token: string) => {
this.logger.debug(`Token generated: ${token}`);
return this.httpClient
.get(`${environment.apiBasePath}/users`, {
headers: { authorization: `Bearer ${token}` },
})
});
}
this.dashboardFacade.getCurrentUserUid().then(uid => {
if(uid) {
this.dashboardFacade.getCurrentUser(uid).subscribe(
(user) => console.log(user)
)
}
});
this.dashboardFacade.getCurrentUserUid().then(uid => {
if(uid) {
this.dashboardFacade.getCurrentUser(uid).subscribe(
(user) => console.log(user)
)
}
});