Ionic3:找不到 'object' 类型的不同支持对象“[object Object]”。 NgFor 仅支持绑定到 Iterables,例如 Arrays

Ionic3 :Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays

TS

this.myservice.online_service('online_service',data).subscribe(res=>{
        this.result = res;

        this.bus_data=this.result.searchResult;
    })

Html

<ion-card  *ngFor= "let bus of bus_data ; let i = index" [attr.data-target]="'#viewseat' + i" (click)="get_seats(i)">
        <ion-card-header>
            <ion-row>
                <ion-col col-6 class="p0 bus_name">{{bus.operatorName}}</ion-col>
 </ion-row>
</ion-card-header>
 </ion-card>

service.ts

online_service(url,data = null )
{

this.show_loader();
return this.http.post(this.webservice_url+ url,  JSON.stringify(data)).map(res=>res.json())
}

如果服务器 return 有多个数据,代码运行良好。但是在服务器 return 单个数据

的情况下会出现此错误

这意味着你需要始终使用数组来绑定 ngFor。您可以创建一个局部变量并将其声明为数组,然后推送结果。

myResult : any = [];

然后,

this.myservice.online_service('online_service',data).subscribe(res=>{
        this.result = res;
        myResult.push(this.result.searchResult);
})

您需要将它们推送到数组中,如下所示。

在你的 component.ts 中定义这样的数组。

bus_data: any[] = [];

现在,在您的数据加载函数中,

this.myservice.online_service('online_service',data).subscribe(res=>{
   this.result = res;
   this.bus_data=[...this.result.searchResult];
})

非常感谢您的回答。感谢你的努力。我忘记检查来自 server.Now 的数组类型的数据,它工作得很好

让我post正确答案

this.myservice.online_service('online_service',data).subscribe(res=>{
      this.result = res;

      this.bus_data=this.result;
 if (this.bus_data.code === '1') {
    if (this.bus_data.searchResult instanceof Array !== true) {
       this.bus_data.searchResult = new Array(this.bus_data.searchResult);
   }}
   console.log(this.bus_data);
  })