0
votes

l am try to get weather data json for wunderground api using ionic 4 . When l try to run this code l got error ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

short json response

{
  "hourly_forecast": [
    {
      "condition": "غائم جزئياً",
      "icon": "partlycloudy",
      "icon_url": "http://icons.wxug.com/i/c/k/partlycloudy.gif",
      "fctcode": "2",
      "sky": "30"
    }
  ]
}

code

weather : any 

constructor(private https: HttpClient) {


  this.https.get('xxxxxxxxxxxx/q/muscat.json')
  .subscribe(data => {

    this.weather= data
    console.log(this.weather)

  })

}

html

   <tr  *ngFor="let item of weather">

        <td>{{item.hourly_forecast.condition}}</td>
      </tr>

any idea please ?

4
You can try something like this: *ngFor="let item of weather.hourly_forecast" and <td>{{item.condition}}</td>yer

4 Answers

3
votes
 <tr  *ngFor="let item of weather.hourly_forecast.condition">
    <td>{{item}}</td>
 </tr>

This one will work

2
votes

Your data is an object, not an array. Array is wrapped in .hourly_forecast field of the object:

<tr *ngFor="let item of weather?.hourly_forecast">
    <td>{{item.condition}}</td>
</tr>

Make sure to add an ? so you don't get an error before the data arrives.

1
votes

Here is the sample code which works!.

var json = {
  "hourly_forecast": [{
      "condition": "غائم جزئياً",
      "icon": "partlycloudy",
      "icon_url": "http://icons.wxug.com/i/c/k/partlycloudy.gif",
      "fctcode": "2",
      "sky": "30"
    },
    {
      "condition": "غائم جزئياً",
      "icon": "partlycloudy",
      "icon_url": "http://icons.wxug.com/i/c/k/partlycloudy.gif",
      "fctcode": "2",
      "sky": "30"
    }
  ],
  "hourly_forecast2": [{
    "condition": "غائم جزئياً",
    "icon": "partlycloudy",
    "icon_url": "http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "fctcode": "2",
    "sky": "30"
  }]
}

// If you want iterate inside

for (var element in json) {
  var data = json[element];
  for (var val in data) {
    console.log(data[val].condition)
  }
}

Or else check whether data has been imported correctly

this.https.get('xxxxxxxxxxxx/q/muscat.json')
 .subscribe(data => {
this.weather = data.hourly_forecast;
 });

Now it will work

<tr  *ngFor="let item of weather">
    <td>{{item.hourly_forecast.condition}}</td>
  </tr>
0
votes

NgFor can only be used on arrays. Try something like this:

*ngFor="let item of weather.hourly_forecast" and <td>{{item.condition}}</td>