angular - 错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性“id”
问题描述
显示我的用户数据时出现此错误。奇怪的是,无论如何都会显示数据,但它会干扰应用程序的运行。
零件
user:any
constructor(public auth: AuthService, private alert: Ion_Alert) {
if(this.auth.isLoggedIn) {
this.user = this.auth.getUserData().subscribe({
next: ( data:any ) => {
this.user = data.data.person
console.log(this.user)
},
error: error => {
this.alert.showAlert("Error!", error.message)
console.error('There was an error!', error)
}
})
} else {
this.alert.showAlert('Error!', "Either you're not logged in or you just haven't verified your email")
}
}
HTML
<ion-content>
<ion-card-content class="ion-text-center">
<h1 *ngIf="user.first_name">{{user.first_name}} {{user.last_name}}</h1>
<h1 *ngIf="user.business_name">{{user.business_name}}</h1>
<h3 *ngIf="user.documents_type.id === 1">DNI: {{user.dni}}</h3>
<h3 *ngIf="user.documents_type.id === 2">NIT: {{user.nit}}</h3>
<ion-text color="medium">
<div style="display: flex;" class="ion-justify-content-center">
<ion-icon name="location-outline" color="medium">
</ion-icon>
<p>{{user.municipio.name | titlecase}}, {{user.municipio.departamento.name}}, {{user.municipio.departamento.country.name}}.</p>
</div>
</ion-text>
<h3>{{user.profession}} - {{user.phone}}</h3>
</ion-card-content>
</ion-card>
</ion-content>
身份验证服务
getUserData() {
return this.http.get(`${this.env.API_URL}/users/firebase/${JSON.parse(localStorage.getItem('user')).uid}`)
}
get isLoggedIn(): boolean {
const user = JSON.parse(localStorage.getItem('user'));
return (user !== null && user.emailVerified !== false) ? true : false;
}
解决方案
在您的 html 中,在?
此处添加:user.documents_type?.id
,
user.documents_type
可能是未定义的。
推荐阅读
- c# - 我如何运行 c# wpf pre Filled form 并使用 selenium 自动将数据插入 Web 表单
- c# - JWT Web 令牌授权验证不提供声明数据
- android - 如何尝试使用动态跨度计数在 GridLayoutManager 中拖放项目
- python - 当我们在其中传递两个矩阵时, sicpy.linalg.eig() 会返回什么?它是否给了我们两个矩阵的同时对角化?
- flutter - 从 Firestore 中的文档中检索数组数据
- python - 如何使用 .loc 方法向前搜索 i 行?
- javascript - 通过自定义组件将 props 添加到 styled-component
- mysql - Node.js / Discord.js - 如何在构建嵌入时正确等待 for 循环完成
- java - 重新启动 Spring Boot 应用程序后,使用 rabbitMQ 从 10 个并发消费者变为 1 个
- visual-studio - 在 Visual Studio 预构建事件中运行 powershell 脚本