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

后端 未结 18 1014
逝去的感伤
逝去的感伤 2020-11-29 05:50

I looked at similar questions, but none of them helped me. I am going to receive an object like the following:

[
  {
    \"id\": 1,
    \"name\": \"Safa\",
          


        
相关标签:
18条回答
  • In your JSOn file, please make below change.

     {
        "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"
          }
        ]
        }
    

    And after that:

     this.http.get(url).map(res:Response) => res.json().data);
    

    The data is actually the name of tge collection of json file. Please try the code above, I am sure it will work.

    0 讨论(0)
  • 2020-11-29 06:14
    Store that objects into Array  and then iterate the Array
    export class AppComponent {
    
     public obj: object =null;
     public myArr=[];
    
      constructor(){
    
        this.obj = {
          jon : {username: 'Jon', genrePref: 'rock'},
          lucy : {username: 'Lucy', genrePref: 'pop'},
          mike : {username: 'Mike', genrePref: 'rock'},
          luke : {username: 'Luke', genrePref: 'house'},
          james : {username: 'James', genrePref: 'house'},
          dave : {username: 'Dave', genrePref: 'bass'},
          sarah : {username: 'Sarah', genrePref: 'country'},
          natalie : {username: 'Natalie', genrePref: 'bass'}
      }
      }
      ngOnInit(){
        this.populateCode();
      }
    
      populateCode(){
        for( let i in this.obj) {   //Pay attention to the 'in'
        console.log(this.obj[i]);
        this.myArr.push(this.obj[i]);
      }
      }
     }
    
    
    
    <div *ngFor="let item of myArr ">
        {{item.username}}
        {{item.genrePref}}
      </div>
    
    0 讨论(0)
  • 2020-11-29 06:15

    My solution is create a Pipe for return the values array or propierties object

    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);
      }
    }
    

    The template Implement

    <button ion-item *ngFor="let element of element_list | valueArray" >
        {{ element.any_property }}
    </button> 
    
    0 讨论(0)
  • 2020-11-29 06:16

    There you don't need to use this.requests= when you are making get call(then requests will have observable subscription). You will get a response in observable success so setting requests value in success make sense(which you are already doing).

    this._http.getRequest().subscribe(res=>this.requests=res);
    
    0 讨论(0)
  • 2020-11-29 06:16

    I had the same error because I have mapped the HTTP response like this:

    this.http.get(url).map(res => res.json);
    

    Note how I accidentally called .json like a variable and not like a method.

    Changing it to:

    this.http.get(url).map(res => res.json());
    

    did the trick.

    0 讨论(0)
  • 2020-11-29 06:19

    Just declare the var as an array in which you holding the data , it worked for me.

    listingdata:Array<any> = [];
    this.listingdata = data.results.rows;
    

    and loop the listingdata on html page

    0 讨论(0)
提交回复
热议问题