找不到'对象'类型的不同支持对象'[object Object]'.NgFor仅支持绑定到诸如Arrays之类的Iterables

Jef*_*eff 52 angular-template angular

我看了类似的问题,但没有一个帮助过我.我将收到如下对象:

[
  {
    "id": 1,
    "name": "Safa",
    "email": "neerupeeru@mail.ee",
    "purpose": "thesis",
    "programme": "Software Engineering",
    "year": 2016,
    "language": "Estonian",
    "comments": "In need of correcting a dangling participle.",
    "status": "RECEIVED"
  },
  {
    "id": 2,
    "name": "Safa",
    "email": "neerupeeru@mail.ee",
    "purpose": "thesis",
    "programme": "Software Engineering",
    "year": 2016,
    "language": "Estonian",
    "comments": "In need of correcting a dangling participle.",
    "status": "RECEIVED"
  },
  {
    "id": 3,
    "name": "Salman",
    "email": "neerupeeru@mail.ee",
    "purpose": "thesis",
    "programme": "Software Engineering",
    "year": 2016,
    "language": "Estonian",
    "comments": "In need of correcting a dangling participle.",
    "status": "RECEIVED"
  }
]
Run Code Online (Sandbox Code Playgroud)

这是我的http服务接收它:

getRequest(){
        return this._http.get("http://consultationwebserver.herokuapp.com/requests").map(res => res.json());
    }
Run Code Online (Sandbox Code Playgroud)

最后,在我用这种方式调用服务:

requests;
    constructor(private _http:requestService){}
    ngOnInit(){
        this.requests=this._http.getRequest().subscribe(res=>this.requests=res);
    }
Run Code Online (Sandbox Code Playgroud)

不幸的是,当页面加载时,它抱怨:

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
Run Code Online (Sandbox Code Playgroud)

那么,这段代码出了什么问题?

Pan*_*kar 24

在那里你不需要this.requests=get打电话时使用(然后requests会有可观察的订阅).您将获得可观察的响应,success因此requests成功的设置 值是有意义的(您已经在做了).

this._http.getRequest().subscribe(res=>this.requests=res);
Run Code Online (Sandbox Code Playgroud)

  • 这不是上面提到的错误的解决方案(无法找到'object'类型的不同支持对象'[object Object]'.)他必须使用dataType,例如ex:`requests:any = []`即使我遇到了同样,我通过添加dataType修复 (13认同)
  • 基本上 NgFor 仅适用于可迭代的项目,例如数组。如果你声明字典并尝试使用 *ngfor 它将出现这些类型的错误。例如,当您像 this.myArray = {} 那样声明数组时。就会出现问题。应该是 this.myArray = []。问题就解决了。 (3认同)

Gün*_*uer 14

this.requests从中移除

ngOnInit(){
  this.requests=this._http.getRequest().subscribe(res=>this.requests=res);
}
Run Code Online (Sandbox Code Playgroud)

ngOnInit(){
  this._http.getRequest().subscribe(res=>this.requests=res);
}
Run Code Online (Sandbox Code Playgroud)

this._http.getRequest()返回订阅,而不是响应值.响应值由传递给的回调分配subscribe(...)


Chr*_*oph 7

我有同样的错误,因为我已经映射了这样的 HTTP 响应:

this.http.get(url).map(res => res.json);
Run Code Online (Sandbox Code Playgroud)

请注意我如何不小心将 .json 称为变量而不是方法。

将其更改为:

this.http.get(url).map(res => res.json());
Run Code Online (Sandbox Code Playgroud)

成功了。


小智 7

在您的JSOn文件中,请进行以下更改.

 {
    "data":
    [
      {
        "id": 1,
        "name": "Safa",
        "email": "neerupeeru@mail.ee",
        "purpose": "thesis",
        "programme": "Software Engineering",
        "year": 2016,
        "language": "Estonian",
        "comments": "In need of correcting a dangling participle.",
        "status": "RECEIVED"
      },
      {
        "id": 2,
        "name": "Safa",
        "email": "neerupeeru@mail.ee",
        "purpose": "thesis",
        "programme": "Software Engineering",
        "year": 2016,
        "language": "Estonian",
        "comments": "In need of correcting a dangling participle.",
        "status": "RECEIVED"
      },
      {
        "id": 3,
        "name": "Salman",
        "email": "neerupeeru@mail.ee",
        "purpose": "thesis",
        "programme": "Software Engineering",
        "year": 2016,
        "language": "Estonian",
        "comments": "In need of correcting a dangling participle.",
        "status": "RECEIVED"
      }
    ]
    }
Run Code Online (Sandbox Code Playgroud)

在那之后:

 this.http.get(url).map(res:Response) => res.json().data);
Run Code Online (Sandbox Code Playgroud)

数据实际上是json文件的tge集合的名称.请尝试上面的代码,我相信它会起作用.


Iva*_*tes 7

我的解决方案是创建一个管道来返回值数组或属性对象

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'valueArray',
})
export class ValueArrayPipe implements PipeTransform {

  // El parametro object representa, los valores de las propiedades o indice
  transform(objects : any = []) {
    return Object.values(objects);
  }
}
Run Code Online (Sandbox Code Playgroud)

模板实现

<button ion-item *ngFor="let element of element_list | valueArray" >
    {{ element.any_property }}
</button> 
Run Code Online (Sandbox Code Playgroud)


小智 6

您可以将书籍(第2行)声明为数组:

  title: any = 'List of books are represted in the bookstore';
  books: any = []; 
  constructor(private service:  AppService){
  }

  ngOnInit(){
    this.getBookDetails();
  }

  getBookDetails() {
    this.service.getBooks().subscribe(books => {
      this.books = books.json();
      console.log(this.books);
    });
  }
Run Code Online (Sandbox Code Playgroud)

  • 必须说,这对我有帮助,因为我将数组初始化为`products = {}`并且应该已经完成​​了`products:any = []`,因为`* ngFor` 1st在一个空的`object'上运行,他不能 (3认同)