HTTP Service And Repeat - NgFor only supports binding to Iterables such as Arrays


#1

Hey all, firstly sorry for my bad english.
Im trying get data from api and list, but when i try it, ionic get this error:

Cannot find a differ supporting object ‘[object Object]’ of type ‘object’. NgFor only supports binding to Iterables such as Arrays.

This is my code:

Home.ts

this.api.getTest().then(success =>{
            this.users = success;
            console.log(this.users);
            loader.dismiss();
});

API Provider

getTest() {
    return new Promise((resolve) => {

      this.http.get('https://randomuser.me/api/?results=10')
      .map(res => res.json())
      .subscribe(data => {
        this._data = data.results;
        resolve(this._data);
      });
    })
  }

<ion-item text-wrap *ngFor="let user of users">

If anyone can help me,
Thanks


#2

It would help to see what is being logged in the console, to know what is being returned.

getTest() does not need to be creating promises, and in fact does not propagate errors correctly as written. Do it this way instead:

getTest(): Observable<User[]> {
  return this.http.get('url').map((rsp) => {
    return extractUsersFromResponse(rsp);
  });
}

this.api.getTest().subscribe((users) => {
  this.users = users;
});

What extractUsersFromResponse looks like depends on what the backend is sending you.