首页 > 解决方案 > ERROR 错误:尝试区分“假”时出错。只允许使用数组和可迭代对象

问题描述

我在 TS 中有这个功能:

getClientContact(clientid: string) {
        if (this.client&& this.client.length > 0) {
        const [filteredClientId] = this.client.filter(pt => pt.client_id === clientid);
        if (typeof filteredClientId !== 'undefined' && clientid === filteredClientId.client_id) {
        return filteredClientId.contactNo;
    }
  }
}
getClientAdd(clientid: string) {
        if (this.client&& this.client.length > 0) {
        const [filteredClientadd] = this.client.filter(pt => pt.client_id === clientid);
        if (typeof filteredClientadd !== 'undefined' && clientid === filteredClientadd.client_id) {
        return filteredClientadd.address;
    }
  }
}

onSelect(clientid) {
            this.selectedClient = null;
            for (let i = 0; i < this.client.length; i++) {
                if (this.client[i].client_id === clientid) {
                    this.selectedClient = this.client[i];
                }

            }
        }

我在 html 中调用它,如下代码:

  <fieldset>
      <legend>Client Data</legend>
      <div class="input-field col s12">
        <select (change)="onSelect($event.target.value)" [(ngModel)]="selectedClient.client_id" formControlName="client_id" id="client_id"
          materialize="material_select" [materializeSelectOptions]="client">
          <option value="" disabled selected>Nome:</option>
          <option *ngFor="let item of client" [value]="item.client_id">{{item.clientName}}</option>
        </select>
      </div>
      <br>
      <div class="input-field col s12">
       Phone:
        <br>
        <span>{{getClientContact(selectedClient.client_id)}}</span>
        <br>
      </div>
      <br>
      <div class="input-field col s12">
        Address:
        <br>
        <span>{{getClientAdd(selectedClient.client_id)}}</span>
        <br>
      </div>
    </fieldset>

在此代码中,ERROR Error: Error trying to diff 'false'. Only arrays and iterables are allowedclient为空时出现此错误。

拜托,你能问我任何想法,我怎样才能修改代码以不显示此错误?这对我来说非常重要。

标签: angulartypescript

解决方案


推荐阅读