auth0 - 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
}
}
所以有人可以帮助我吗?谢谢
解决方案
您可以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
推荐阅读
- python - scikit-learn 中的目标转换和特征选择
- javascript - React Infinite Scroll 超过了最大更新深度
- scrapy - 为什么scrapy splash返回我原来的网址?
- sql-server - 当我从 sql server 设置 list.recordset 时,以最大化模式摇动我的访问表单
- flutter - 如果在自定义初始屏幕上看到检查,我该怎么办?如果已经看到演练屏幕
- php - 我试图将数据库中的数据显示到表中,但它不返回任何内容
- angular - 如何更改 Angular 材质步进器动态创建的组件中的状态图标?
- python - 在 OSX 上使用 Python 关闭选项卡
- java - HashMap 按 AtomicInteger 的值排序
- angular - 比较 2 个输入字段的值以通过模板验证 Angular 7 中的表单