错误错误:未捕获(承诺):类型错误:无法读取未定义的属性“id”

时间:2021-01-13 18:49:27

标签: angular typescript firebase ionic-framework

显示我的用户数据时出现此错误。奇怪的是,数据无论如何都会显示,但它会导致应用程序出现故障。

see image right here

组件

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;
  }

1 个答案:

答案 0 :(得分:1)

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

user.documents_type 可能未定义。