首页 > 解决方案 > auth0 获取后台配置文件信息

问题描述

我正在尝试从 Auth0 获取用户个人资料信息。它与管道异步在前端工作

<pre *ngIf="auth.userProfile$ | async as profile">
<code>{{ profile | json }}</code>
</pre>

但是在后端如果我想读取昵称并用它做点什么。我迷路了。

constructor(public auth: AuthService)
ngOnInit() {
   if (this.auth.isAuthenticated$) {
    const result = this.auth.userProfile$;
   }
}

我知道我的变量“结果”是一个 Observable。但我对 Observable 的这些东西很陌生。我试图获得价值。如果我使用调试控制台,我可以看到这一行的值:

this.auth.userProfile$.source.value.nickname

但是如果我在我的代码中写了它,我有这个错误:Typescript error: Property 'value' does not exist on type 'Observable'

ngOnInit() {
   if (this.auth.isAuthenticated$) {
    const result = this.auth.userProfile$;
    console.log(this.auth.userProfile$.source.value.nickname); // error here

   }
}

所以有人可以帮助我吗?谢谢

标签: auth0

解决方案


您可以observable通过订阅它来访问其中的数据(这就是async管道在后台所做的事情)。

以下是如何订阅您的 observable 的示例:

// import { tap } from 'rxjs/operators';
// tap is one of many rxjs operators
// operators allow you to perform operations on data within observables

this.auth.userProfile$.pipe(
  tap(profile => {
    console.log(profile);
  })
)
.subscribe(); // this is what allows you access the data within the observable

可观察的:https ://angular.io/guide/observables

RXJS 运算符:https ://rxjs-dev.firebaseapp.com/guide/operators


推荐阅读