订阅者返回订阅者不是信息

时间:2021-05-09 20:06:47

标签: angular observable

我正在尝试访问我的服务中的 http 请求的结果:

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

但是,console.logging 用户配置文件会返回订阅者,但不会返回我要查找的信息。我如何获取信息?

  ngOnInit() {
    this.userProfile = this.userService.getUserProfile().subscribe((res) => {
      this.userProfile = res;
    });
    console.log(this.userProfile);
  }

1 个答案:

答案 0 :(得分:2)

不要将 userProfile 设置为订阅,而是仅在订阅回调中设置。

ngOnInit() {
  this.userService.getUserProfile().subscribe((res) => {
    this.userProfile = res;
    console.log(this.userProfile);
  });
}