0

I am trying to have access to the result of an http request in my service:

  getFriendsProfile(email) {
    return this.http.get(environment.apiBaseUrl + '/profile/' + email);
  }

However, console.logging the user profile returns a subscriber but not the information I am looking for. How do I get the info?

  ngOnInit() {
    this.userProfile = this.userService.getUserProfile().subscribe((res) => {
      this.userProfile = res;
    });
    console.log(this.userProfile);
  }
Jonathan
  • 441
  • 1
  • 9
  • 28
  • Maybe this answers your question? https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular – eko May 09 '21 at 20:13

1 Answers1

2

Do not set the userProfile to the subscription, instead only set it in the subscription callback.

ngOnInit() {
  this.userService.getUserProfile().subscribe((res) => {
    this.userProfile = res;
    console.log(this.userProfile);
  });
}
Ben L
  • 694
  • 5
  • 13
  • your answer is correct, however you have typed `Do not set the userProfile to the subscription, instead only set it in the subscription callback.`. Correctly is do not use console.log outside of subscription but only inside as there it will be called with the value c orrectly – Panagiotis Bougioukos May 09 '21 at 20:27
  • If you set this.userProfile = this.userService.getUserProfile().subscribe(...) you are not setting it to the result but rather the returned subscription. There is more going on here than moving a console log. – Ben L May 10 '21 at 01:32