Getting an object array from an Angular service Getting an object array from an Angular service arrays arrays

Getting an object array from an Angular service


Take a look at your code :

 getUsers(): Observable<User[]> {        return Observable.create(observer => {            this.http.get('http://users.org').map(response => response.json();        })    }

and code from https://angular.io/docs/ts/latest/tutorial/toh-pt6.html(BTW. really good tutorial, you should check it out)

 getHeroes(): Promise<Hero[]> {    return this.http.get(this.heroesUrl)               .toPromise()               .then(response => response.json().data as Hero[])               .catch(this.handleError);  }

The HttpService inside Angular2 already returns an observable, sou don't need to wrap another Observable around like you did here:

   return Observable.create(observer => {        this.http.get('http://users.org').map(response => response.json()

Try to follow the guide in link that I provided. You should be just fine when you study it carefully.

---EDIT----

First of all WHERE you log the this.users variable? JavaScript isn't working that way. Your variable is undefined and it's fine, becuase of the code execution order!

Try to do it like this:

  getUsers(): void {        this.userService.getUsers()            .then(users => {               this.users = users               console.log('this.users=' + this.users);            });    }

See where the console.log(...) is!

Try to resign from toPromise() it's seems to be just for ppl with no RxJs background.

Catch another link: https://scotch.io/tutorials/angular-2-http-requests-with-observables Build your service once again with RxJs observables.