0

I have a trivial problem with my http request. I have this service:

export class DataService {

  constructor(private http: Http) { }

  getUsers():Observable<Users>{
    return this.http.get(url).map(this.extractData);
  }    

  extractData(res:Response){
    return res.json();
  }

}

This method must return a list of users. Now, if I do this:

users: Users;

ngOnInit() {
    this.dataService.getUsers().subscribe(data=>
       this.users=data
    );
    console.log(this.users)
}

it prints undefined, instead if I do this:

ngOnInit() {
        this.dataService.getUsers().subscribe(data=>console.log(data));
  }

it prints the result. How Can I associate the result to a variable? Is there a problem with the type of variable?

Thank you

n00dl3
  • 21,213
  • 7
  • 66
  • 76
dstyle
  • 68
  • 9

0 Answers0