0

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 ?

1
  • 2
    You can try something like this: *ngFor="let item of weather.hourly_forecast" and <td>{{item.condition}}</td> Commented Mar 6, 2019 at 14:43

4 Answers 4

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

This one will work

Sign up to request clarification or add additional context in comments.

2 Comments

*ngFor="let item of weather.hourly_forecast.condition" 'cause this is the array so you can loop thru that
2

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.

Comments

1

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>

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.