【问题标题】:Once Again NgFor only supports binding to Iterables such as ArraysOnce Again NgFor 仅支持绑定到 Iterables,例如 Arrays
【发布时间】:2017-09-07 18:15:14
【问题描述】:

和我之前的很多人一样,我有这个错误:

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

我尝试了许多解决方案,例如: error display in *ngfor json array object

但是没有任何效果。

  public getInterfaces(): Observable<InterfaceBrief[]> {
    let headers = this.createBasicHeaders();
    let options = new RequestOptions({
      method: 'get',
      headers: headers});
    let url = this.restApi +"/dashboard/list";
    console.log(url);
    return this.http.get(url, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  private extractData(res: Response) {

    let body = res.json();
    console.log("GOOD");
    return body.data || {};
  }

export class DashboardComponent implements OnInit {

  errorMessage: string;
  interfacesBrief: InterfaceBrief[];

  constructor(private _service: AuthService, private _emotService: EmotClientService) {
  }
  ngOnInit() {
    this.getInterfaces();
  }
  getInterfaces() {
    this._emotService.getInterfaces().subscribe(

      data => this.interfacesBrief = data,
      error => this.errorMessage = <any>error
    );
  }
}

当我改变时:

    return body.data || {}; 

到:

return body.data.items || {};

我有错误:

Cannot read property 'items' of undefined

ERR:无法读取未定义的属性“数据”

【问题讨论】:

  • 请发布包含*ngFor的html代码
  • 如果body.data 没有items,它看起来与*ngFor 无关。
  • 你的 JSON 是什么样子的?

标签: angular rest-client


【解决方案1】:
  • 只返回res.json() (为什么有时如果没有正确获取数据而不是 angular 会在这里抛出错误)。

  • 尝试在 HTML 中使用 elvis 运算符(安全导航)。

试试这个代码

  private extractData(res: Response) {

    let body = res.json();
    console.log("GOOD");
    return body || {};
  }

  export class DashboardComponent implements OnInit {
   ...............

  getInterfaces() {
    this._emotService.getInterfaces().subscribe(

      data => {
        this.interfacesBrief = data
        console.log(this.interfacesBrief ,"Without Error")
      },
      error => this.errorMessage = <any>error
    );
  }
}

<div *ngFor='let items of interfacesBrief?.data?.items'>
  {{items}}
</div>

【讨论】:

  • 在控制台日志中我看到 "[Object, Object] "Without Error" 。但元素不是 int 视图。作为回应,我看到该列表正确地取自 rest api
  • 尝试在这里安慰console.log(body , "GOOD");
  • Array[2] 0 : 对象 1 对象客户端: "client1" countKO: 3 (...) 。所以没关系
【解决方案2】:

根据评论,当您 console.log 您的回复时:Array[2] 0 : Object 1 Object client: "client1" countKO: 3 (...) 您的回复中显然没有 data 对象,因此您应该按原样返回回复。

private extractData(res: Response) {
  let body = res.json();
  return body || []; // here!
}

当您收到回复后,您只需迭代该数组并显示您想要的属性,如下所示:

<div *ngFor='let item of interfacesBrief'>
  {{item.client}} - {{item.countKO}} 
  <!-- print whatever else properties you have and want to display -->
</div>

【讨论】:

  • 它有效。但是你能解释一下,为什么它不像官方文档那样工作吗?为什么数据不存在??在其他请求(一项项目的帖子)中,它正在处理正文?
  • 您没有包含数组的对象data,在响应中您得到的只是一个数组。您的回复看起来像这样:[{object1 content},{object2 content}] 而不是这样:{data:[{object1 content},{object2 content}]}
  • 响应来时的样子,完全取决于您从后端发送的内容和结构:)
  • 但我的后端从不发送 {data: ...} 但对于一项它正在工作...所以??
  • 嗯,您收到的响应是从后端设置的。时期。前端和后端之间不会发生任何神奇的事情来改变您的响应方式。数据的外观如何在后端设置。只需检查后端,例如,控制台在发送之前将数据记录在后端。
猜你喜欢
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多