I'm pretty new to both Ionic and Angular so please excuse if this question doesn't make much sense.
Platform : Ionic 3.
I have a provider : rest.ts as follows
getLights() {
return new Promise(resolve => {
this.http.get(this.apiUrl+'/lights').subscribe(data => {
resolve(data);
}, err => {
console.log(err);
});
});
}
This returns the following data (sensitive info removed)
{
"1": {
"state": {
"on": false,
"bri": 98,
"alert": "none",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Dimmable light",
"name": "Lion",
"modelid": "LWB010",
"manufacturername": "Philips",
},
"2": {
"state": {
"on": true,
"bri": 100,
"alert": "none",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Dimmable light",
"name": "Brutus",
"modelid": "LWB010",
"manufacturername": "Philips",
},
"3": {
"state": {
"on": true,
"bri": 254,
"hue": 8418,
"sat": 140,
"effect": "none",
"xy": [
0.4573,
0.41
],
"ct": 366,
"alert": "none",
"colormode": "ct",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": null
},
"type": "Extended color light",
"name": "Hue lightstrip plus 1",
"modelid": "LST002",
"manufacturername": "Philips"
}
}
An ionic page 'list' made up of : list.ts
export class ListPage {
lights: any;
icons: string[];
constructor(public navCtrl: NavController,
public navParams: NavParams,
public restProvider: RestProvider) {
this.getLights();
}
getLights() {
this.restProvider.getLights()
.then(data => {
this.lights = data;
console.log(this.lights);
});
}
}
and list.html :
<ion-content>
<ion-list inset>
<ion-item *ngFor="let light of lights">
<h2>{{light.name}}</h2>
<p>{{light.type}}</p>
</ion-item>
</ion-list>
</ion-content>
Although the console log statement displays the JSON correctly, I can't iterate it using ngFor in list.html due to the following error.
Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
I realise I need to convert the JSON into a javascript array, but not sure where or how to do this. Any pointers would be very much appreciated.