如何使用Angular查询和显示子集合?
我正在开发我的第一个angular/firestore应用程序,现在我可以在没有子集合的情况下显示单个集合中的文档数据。我试图在此基础上展开并显示子集合中的数据。我的firestore数据库如下所示 用户->用户ID->战斗->战斗ID 我现在试图在我的页面上显示战斗,所以我试图查询战斗的子部分,并将其编码到我的组件中 我的TS:如何使用Angular查询和显示子集合?,angular,google-cloud-firestore,angularfire2,Angular,Google Cloud Firestore,Angularfire2,我正在开发我的第一个angular/firestore应用程序,现在我可以在没有子集合的情况下显示单个集合中的文档数据。我试图在此基础上展开并显示子集合中的数据。我的firestore数据库如下所示 用户->用户ID->战斗->战斗ID 我现在试图在我的页面上显示战斗,所以我试图查询战斗的子部分,并将其编码到我的组件中 我的TS: @组件({ 选择器:“应用我的战斗”, templateUrl:'./my.fights.component.html', 样式URL:['./my.componen
@组件({
选择器:“应用我的战斗”,
templateUrl:'./my.fights.component.html',
样式URL:['./my.component.css']
})
导出类MyFightsComponent实现OnDestroy,OnInit{
userId:any;
可观察的;可观察的;
用户$:可观察;
用户:任何;
私人只读订阅:订阅;
fightCollection:AngularFirestoreCollection;
构造函数(私有afs:AngularFirestore,私有firestoreData:FirestoreDataService,公共授权:
AuthService,私人fightTest:FightService){
this.subscription=auth.user$.subscripte(user=>{
this.userId=user.uid
this.fightCollection=this.afs.collection(`users/${this.userId}/Fights`);
this.fights$=this.fightCollection.valueChanges();
});
}
恩戈尼尼特(){
}
恩贡德斯特罗(){
this.subscription.unsubscripte();
}
}
用户ID是从我创建的注入式身份验证服务中提取的。我将userID放入一个变量,只是为了测试我是否能够正确地提取uid。然后,我尝试将userId值编码到正确的firestore路径中,以提取该用户集合并将其分配给可观察对象
我的HTML:
战斗,蓝战士;
为了简单起见,我现在只是想拉场上的蓝战斗机
我没有收到任何错误,但也没有显示任何内容。我不知道我是否正确地阅读了这个集合,或者只是没有正确地用HTML调用它。尝试改变构建观察对象的方式。不要在订阅中定义它们。使用更高的可观察值
。。。
this.fights$=auth.user$.pipe(
点击(user=>console.log('is user emitting?:',user)),
以(1)为例,
合并映射(用户=>{
this.userId=user.uid
this.fightCollection=this.afs.collection(`users/${this.userId}/Fights`);
返回此.fightCollection.valueChanges();
}),
轻触(战斗=>console.log('战斗值:',战斗))
);
...
添加tap
操作符有助于您了解您的观测值是否确实在发射某些东西
另外,我认为您的html中有一个输入错误,应该是:
-
{{战斗,蓝战士}
这是否回答了您的问题?