angular - AngularJs 无法读取未定义的属性“订阅”
问题描述
我想在应用程序中访问我记录的用户数据,但我收到了这个错误
错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性“订阅”
这是我的代码:
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”类型错误:无法读取未定义的属性“token_type”
当我的用户登录时,令牌存在于数据库中
解决方案
解决了
这是我的最终代码,它解决了我的问题
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;
});
}
});
}
希望能帮助到你。
推荐阅读
- neural-network - 如何使用 Pytorch 的 Autograd 来反向传播多个层(矩阵)?
- reactjs - 通过对象数组反应本机映射给出空白值
- r - 使绘图水平的y轴标题
- r - 将每日日期向量转换为 R 中的周数
- ripple - 对瑞波币 XRP 网站的惊人影响
- unit-testing - CosmosDB 错误 429 单元测试:请求率很大
- c# - 如何解码 MVC5 中的整数?
- c# - 使用 IdentityServer4 保护我的网站和 API
- angular - Angular6 / AWS Amplify - Subscribe() 无法从 fromPromise() 捕获错误
- android - 对于离线访问数据,最好使用 Room 库或 HTTP 缓存控制?