【问题标题】:ERROR TypeError: Cannot read property 'documents' of undefined错误类型错误:无法读取未定义的属性“文档”
【发布时间】:2020-03-27 16:12:07
【问题描述】:

ERROR TypeError: Cannot read property 'documents' of undefined - in ionic 5。我不知道如何解决这个问题。

在我的 HTML 文件中

<ion-row>
  <ion-col class="con-txt" >Document No.</ion-col>
  <ion-col class="con2-txt">{{showDocumentNoBasedOnPriority()}}</ion-col>
</ion-row>

在我的 TS 文件中。

showDocumentNoBasedOnPriority() {
  let documentNoList: any;
  if (this.patientDetails.documents) {
    documentNoList = this.patientDetails.documents;
    let priorityCodes: { [id: string]: number } = {};
    priorityCodes.ID = 0;
    priorityCodes.PP = 1;
    priorityCodes.WP = 2;
    priorityCodes.FREE = 3;
    priorityCodes.X2 = 4;
    priorityCodes.X1 = 5;
    let results = documentNoList.sort((first, second) => {
      return priorityCodes[first.code] - priorityCodes[second.code];
    });
    return results[0].value;
  } else {
    return '-';
  }
}

编辑

this.api.getPatientData(this.branchId, this.prnNumber)
  .subscribe((patientData) => { 
    console.log('INFO: Patient Data -> ', patientData); 
    if (patientData) { 
      this.patientDetails = patientData; 
      console.log(this.patientDetails); 
      this.loading.dismiss(); 
    } else { 
      console.log('No Patient Records '); 
      this.loading.dismiss(); 
}

【问题讨论】:

  • 你能说明this.patientDetails是如何用数据初始化的吗?
  • this.api.getPatientData(this.branchId , this.prnNumber).subscribe((patientData) => { console.log('INFO: Patient Data -> ', patientData); if (patientData ) { this.patientDetails = patientData; console.log( ); this.loading.dismiss(); } else { console.log('No Patient Records'); this.loading.dismiss(); }

标签: ionic-framework


【解决方案1】:

您的问题可能是由异步数据引起的。 patientData 不是直接设置的,但您的函数 showDocumentNoBasedOnPriority 会立即检查它。因此,当您尝试 this.patientDetails.documents 时,this.patientDetails 未填充来自您的 getPatientData() 的数据并返回 undefined。

更好的方法是在您收到数据后触发您的函数。

documentNumber: string;

ionViewWillEnter() {
  // when we enter the page, the `getPatientData` function is called
  this.getPatientData();
}

getPatientData() {
  this.api.getPatientData(this.branchId, this.prnNumber)
    .subscribe((patientData: object) => { 
      // If your check for patientData and it returns a empty object, this will still
      // pass. An empty object is truthy in JS. You can surpass this by checking if
      // the object is filled with more than 0 keys.
      if (Object.keys(patientData).length > 0) {
        this.patientDetails = patientData; 
        // now we trigger the function when it matters
        this.showDocumentNoBasedOnPriority();
      } else {
        // when no records are found, the variable is set to '-'
        this.documentNumber = '-';
      }

      this.loading.dismiss(); 
  }
}

showDocumentNoBasedOnPriority() {
  let documentNoList: any;
  if (this.patientDetails.documents) {
    documentNoList = this.patientDetails.documents;
    let priorityCodes: { [id: string]: number } = {};
    priorityCodes.ID = 0;
    priorityCodes.PP = 1;
    priorityCodes.WP = 2;
    priorityCodes.FREE = 3;
    priorityCodes.X2 = 4;
    priorityCodes.X1 = 5;
    let results = documentNoList.sort((first, second) => {
      return priorityCodes[first.code] - priorityCodes[second.code];
    });

    this.documentNumber = results[0].value;
  } else {
    this.documentNumber = '-';
  }
}

显示documentNumber。您可以随时更改您想在上面的代码中更新 documentNumber 变量的时间和地点。

<ion-row>
  <ion-col class="con-txt" >Document No.</ion-col>
  <ion-col class="con2-txt">{{ documentNumber }}</ion-col>
</ion-row>

【讨论】:

  • 乐于助人!您始终可以接受“关闭”问题的答案;)
猜你喜欢
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 2021-11-22
  • 2014-01-14
  • 2018-07-12
相关资源
最近更新 更多