ERROR TypeError: Cannot read property 'success' of undefined
at Object.eval [as updateRenderer] HomeComponent.html:1
控制台日志(用户详细信息);得到这个https://i.imgur.com/HNZ5PRg.png
该应用程序编译成功,在 Home Component HTML 中,它正确打印了 first_name,但控制台仍然显示我上面提到的错误。
HTML文件
<div *ngIf="isLoggedIn">
{{userDetail.success.first_name}}
</div>
打字稿文件
ngOnInit() {
this.chk.checkLogin();
this.chk.isUserLoggedIn.subscribe( (val) => {
if(val) {
this.isLoggedIn=val;
}
})
//get user profile
this.chk.getUserProfile().subscribe((res: any)=> {
this.userDetail = res;
console.log(this.userDetail);
});
认证服务文件
//Get User Profile
getUserProfile() {
return this.http.post(this.userProfileApi, null);
}
当您从异步请求中获取配置文件时,由于数据不可用,将抛出错误,您可以使用safe navigation operator
(?)处理它,如下所示
<h2 *ngIf="isLoggedIn">
{{userDetail?.success?.first_name}}
</h2>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句