首页 > 解决方案 > 错误错误:未捕获(承诺中):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;
  }

标签: angulartypescriptfirebaseionic-framework

解决方案


在您的 html 中,在?此处添加:user.documents_type?.id,

user.documents_type可能是未定义的。


推荐阅读