angular - Angular 不会在异步任务中显示任何类型的 Observable userdata
问题描述
我的 home.component.html 不会显示我的 userData:
<h1 class="display-4">Hello, {{ (userData | async)?.username }}</h1>
这是来自 home.component.ts 的调用:
ngOnInit(): void {
this.userData = this.userService.getUser();
}
这是服务:
export class UserService {
private userDoc: AngularFirestoreDocument<any>;
user: Observable<Userdata>;
constructor(private authService: AuthService, private afs: AngularFirestore ) {
this.userDoc = this.afs.doc<any>('users/'+ this.authService.getUserId());
this.user = this.userDoc.valueChanges();
}
getUser(){
return this.user
}
这是身份验证服务:
constructor(public afAuth: AngularFireAuth,
public router: Router,
private firestore: AngularFirestore) {
console.log("Constructor for Auth Service")
this.afAuth.authState.subscribe(userData => {
if (userData) {
this.router.navigate([''])
this.user = userData;
if(this.user.emailVerified == false){
this.user.sendEmailVerification()
}
}
})
}
getUserId(){
if(this.user){
return this.user.uid;
} else {
return null;
}
}
但 html 中没有显示任何内容:
这是我在 home 组件中记录用户数据时得到的:
希望有人可以帮忙
解决方案
您可以尝试在控制台中打印数据
this.user = this.afs
.collection<any>('users/'+ this.authService.getUserId())
.valueChanges()
.pipe(
tap(ref => console.log(ref)
);
推荐阅读
- sql - 使用 CAST FLOAT64 时仍然收到被零除的错误
- python - 导入文件时出错:ModuleNotFoundError: No module named
- java - 如何通过反射获取和初始化字段?
- c++ - 执行 while 循环未按预期执行
- protractor - 如何在 Protractor 的 onPrepare 中同时使用函数和规范文件?
- typescript - Typescript 在不使用泛型类型的情况下推断泛型类型
- google-colaboratory - 安装谷歌驱动器时出现谷歌colab错误
- python - 即使使用说明,熊猫中的 SettingWithCopyWarning
- postgresql - liquibase 无法在分区表上创建索引和唯一约束
- reactjs - 传递空数组时如何将useState与Typescript一起使用