Solution 1 :

ngFor can only iterate over an array, It means that the result has to be an array! it cannot be undefined or null which are not iterable.

so when using ngFor you need to have either an empty array or a populated array.

Make sure your service properly returns an array by console loggin’ the result.

this.serviceCovid.recupererHistorique(his.id).subscribe(histo => console.log(histo));

If your service may return undefined compare your value to an empty array.

historique: Historique[] = [];

Solution 2 :

ok it worked
i had the wrong id it his.idCitoyen not his.id

 surChangementHistorique(his: Historique){
    this.serviceCovid.recupererHistorique(his.idCitoyen).subscribe(histo => this.historique = histo);

Solution 3 :

ngFor only supports Iterables such as Array, so you cannot use it for Object.

Problem :

I keep getting this error and I don’t know what to do:
ERROR Error: Cannot find a differ supporting object ‘[object Object]’ of type ‘object’. NgFor only supports binding to Iterables such as Arrays

this is my service

 recupererHistorique(idCitoyen:number): Observable<Historique[]>{ 

    let url = 'http://localhost/tpSyn/getHistoriqueCitoyen.php?idCitoyen=' + idCitoyen;
    return this.http.get<Historique[]>(url); 
  } 
  

this is the component

export class HistoriqueComponent implements OnInit {

  
 @Input() historiquee:Historique;
 historique: Historique[]; 
 selectionActive:boolean = true;
 
  constructor(private serviceCovid: CovidTraceService) { }

  ngOnInit(): void {
  }

  surChangementCitoyen(){
    
  //this.historiquee = null;
  

  }

  
  surChangementHistorique(his: Historique){
    this.serviceCovid.recupererHistorique(his.id).subscribe(histo => this.historique = histo);
    
  
  }
 
}

And the HTMl

  <section *ngIf="historiquee" >
    
    <div >
        <ol >
            <li *ngFor='let hiss of historiquee'> {{hiss.date}}: {{hiss.description}} </li>
        </ol>
    </div>

     
    </section>

In the HTML if i use this instead it works but it shows one line and i want it to show all of the other lines thats why i want to use a *ngFor

  <div>
            <ol>
                <h3>
                    <li> {{historiquee.date}}: {{historiquee.description}} </li>
                </h3>

            </ol>
        </div>

enter image description here

Comments

Comment posted by JWP

There is nothing in the array. Hide the ngFor until after data is there.

Comment posted by Karim

how do i hide it?

Comment posted by Jorge Mussato

Can you

Comment posted by Karim

when i console.log it says Undefined

Comment posted by Karim

when i console.log it it shows [ ] @Aviad

Comment posted by Aviad

try to initialize the value with empty array and see if it solves the problem historique: Historique[ ] = [ ];

Comment posted by Aviad

can you post the returned value ? It seems you’re trying to access an object key inside an array. did you make sure you always get the keys date and description?

Comment posted by Karim

this is the data that i want to return [ { “id”: “482”, “dateInfec”: null, “idCitoyen”: “2952”, “description”: “en test”, “date”: “2020-11-01” }, { “id”: “483”, “dateInfec”: null, “idCitoyen”: “2952”, “description”: “neutre”, “date”: “2020-11-06” } ]

Comment posted by Aviad

try to console log the next thing and make sure you get the same result : “` this.serviceCovid.recupererHistorique(his.id).subscribe(histo => {this.historique = histo; console.log(this.historique)}) “`; validate the you array is stored properly.

By