我想访问我在应用程序中记录的用户数据,但出现此错误
错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“ subscribe”
这是我的代码:
public user: User;
ngOnInit() {
this.authService.user().subscribe((data) => {
this.user = data;
});
}
我的authService.user()
函数是:
user() {
if (this.isLoggedIn === true) {
const headers = new HttpHeaders({
Authorization : this.token.token_type + ' ' + this.token.access_token,
Accept: 'application/json, text/plain',
'Content-Type': 'application/json'
});
return this.http.post<User>(this.env.USERS_URL, null, { headers })
.pipe(
tap(user => {
return user;
})
);
}
}
有什么想法吗?
如果我评论isLoggedIn
,我会得到
未捕获(承诺):TypeError:无法读取未定义的属性'token_type'
当我的用户登录时令牌已存在于数据库中
答案 0 :(得分:0)
这是我解决问题的最终代码
ngOnInit() {
this.authService.getToken().then(() => { //check if user has token in DB
if(this.authService.isLoggedIn) { // check if user is logged
this.authService.user().subscribe(user => { // return user data
this.user = user;
});
}
});
}
希望有帮助。