I'm trying to display data from my json file but I have an error that I can't find a solution to, despite my research on stackoverflow.
i have this error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed
json
{
"flags": [
{
"id": 1,
"mnemo": "KHM",
"libelle": "Cambodge",
"bigramme": "KH"
},
{
"id": 2,
"mnemo": "KHM",
"libelle": "France",
"bigramme": "FR"
},
]
}
ts.file
export class HomepageComponent implements OnInit {
postArray: Ipost[] = [];
constructor(private homeService: HomeService, private router: Router) { }
ngOnInit(): void {
this.getAllNations();
}
getAllNations() {
this.homeService.getAll().subscribe(result => {
this.postArray = result;
console.log(this.postArray);
});
}
}
html
<div class="col-1 px-0" *ngFor="let post of postArray">
<div *ngIf="post.id >= 13">
<span>{{post.libelle}}</span>
</div>
</div>
interface
export interface Ipost {
id:number,
mnemo:string,
libelle:string,
bigramme:string
}
flagsproperty of thepostArrayvariable. Try:*ngFor="let post of postArray?.flags". The safe navigation operator?.will check if thepostArrayvariable is defined before accessing it's propertyflag. - Michael Dflagsproperty in the subscription:this.postArray = result['flags']. In this case, the template doesn't need any change. - Michael D