Consume REST IONIC3

Asked

Viewed 388 times

0

hello, I am new in the area of Web and Ionic3 and I have a problem, I followed a tutorial to consume a API Rest, I managed to bring the list with users, however I want to bring a specific user and show on screen, if I change the URL to the user it shows me this error

TabCepPage.html:10 ERROR Error: Cannot find a differ supporting object
'[object Object]' of type 'Leanne Graham'. NgFor only supports binding 
to  Iterables such as Arrays.

my app is like this:

Preview: Rest.ts

apiUrl = 'https://jsonplaceholder.typicode.com';

constructor(public http: HttpClient) {
  console.log('Hello RestServiceProvider Provider');
}

getUsers() {
  return new Promise(resolve => {
    this.http.get(this.apiUrl+'/users/1').subscribe(data => {
      resolve(data);
    }, err => {
      console.log(err);
    });
  });
}

ts of the page:

users: any;

constructor(public navCtrl: NavController, public restProvider: RestProvider) {
  this.getUsers();
}

getUsers() {
  this.restProvider.getUsers()
  .then(data => {
    this.users = data;
    console.log(this.users);
  });
}

and in HTML I display like this:

<ion-item *ngFor="let user of users">
  <h2>{{user.name}}</h2>
  <p>{{user.email}}</p>
</ion-item>

Someone can help me?

  • please anyone could help? I’m making an app for a company and my job depends on it!

1 answer

1


If you look at the error calmly you will see that Ngfor is trying to iterate over an object 'Leanne Graham' that there is no support and that is the name attribute returned by the API. As you are only returning an object, your html will look like this:

HTML

<ion-item>
  <h2>{{user?.name}}</h2>
  <p>{{user?.email}}</p>
</ion-item>

Obs: if you use Ctrl+Shift+J, you will open the browser console(Chrome), and you can see how the object is being returned, through the console.log().

  • Thanks for the reply friend, but I’m not able to display yet, Chrome reports this error: ERROR Typeerror: Cannot read Property 'name' of Undefined at Object.Val [as updateDirectives] and that ERROR Error: Uncaught (in Promise): Typeerror: Cannot read Property 'name' of Undefined Typeerror: Cannot read Property 'name' of Undefined

  • what appears on the console when you call getUsers()? Maybe you’ll have to take the first item of the array like this: this.users = data[0];

  • hello, I’m trying to get the single user, there’s no array, he’s getting this: {id: 1, name: "Leanne Graham", username: "Bret", email: "[email protected]", address: {...}, ... } address : {street: "Kulas Light", suite: "Apt. 556", city: "Gwenborough", zipcode: "92998-3874", geo: {...}} company : {name: "Romaguera-Crona", catchphrase: "Multi-layered client-server neural-net", Bs: "Harness real-time e-markets"} email : "[email protected]" id : 1 name : : "Leanne Graham" phone : "1-770-736-8031 x56442" username : "Bret" website : "Hildegard.org" ;proto : Object

  • it is, this here solves your problem this.users = date[0]

  • i did this: getUsers() { this.restProvider.getUsers() . then(data => { this.users = data[0]; console.log(this.users); }); } ai aparece ERROR Typeerror: Cannot read Property 'name' of Ufinended at Object.Eval [as updateDirectives] and in getusers() it returns me Undefined

  • edited the answer, really do not need to put the [0], I get confused, now it will work. The operator ? serves to check if the values are present.

  • Thank you very much friend, I just needed to change to "users?." instead of user, helped me a lot, if possible and it was not bother for you would have some contact for me to ask some questions in other problems with Ionic?

  • Good evening, on my profile here of stackoverflow has my Linkedin, can contact me by la.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.