【发布时间】:2020-01-02 18:48:07
【问题描述】:
目前我正在从我的组件 ts 文件中获取一个 firebase 文档。它运行良好。但是,当我试图将逻辑移动到我的服务文件时,然后尝试从我的组件中的服务订阅该方法时出现错误属性订阅在类型 void 上不存在。因为我认为 onAuthStateChanged 方法返回 void。在这种情况下如何获取当前登录的用户 uid。
Service.ts -
getPetsForCurrentUser(){
this.afAuth.auth.onAuthStateChanged((user) =>{
if(user){
// this.afs.collection('pets', ref => ref.where('OwnerID', '==', user.uid)).
this.afs.collection('pets', ref => ref.where('OwnerID', '==', user.uid)).snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return { id, ...data };
}))
).subscribe(docs => {
// loop through each item in the array and log value
docs.forEach(doc => {
return doc;
});
});
}else {
console.log("No user logged in");
}
});
Component.ts -
public allPets : any = [];
constructor(private petservice : PetService) { }
ngOnInit() {
this.petservice.getPetsForCurrentUser().subscribe(res => console.log(res));
}
【问题讨论】:
-
为什么不能在组件内部调用
this.afAuth.auth.onAuthStateChanged((user) =>{? -
那我将如何检查 OwnerID === user.uid?
标签: angular firebase google-cloud-firestore