使用 Angular firestore 返回 firebase 中的子集合 [英] Return a sub-collection in firebase using Angular firestore
问题描述
我有一个名为保存为每个用户的嵌套集合的子集合.用户有自己的字段和保存的集合,其中包含两个字段和一个数组.我只能显示用户字段,但我不知道访问每个用户子集合的正确方法.
导出类 SavedComponent 实现 OnInit{公共保存:已保存[];公共当前用户:任何=空;公共用户:用户;私人订阅:订阅[] = [];构造函数(//添加保存的服务私有的savedService:SavedService,私人身份验证:AuthService,私有加载服务:加载服务,私人路线:ActivatedRoute,私有数据库:AngularFirestore){//this.loadingService.isLoading.next(true);}this.subscriptions.push(this.auth.currentUser.subscribe( 用户 => {this.currentUser = 用户;this.loadingService.isLoading.next(false);//控制台记录当前已认证的用户控制台日志(this.currentUser);}));this.subscriptions.push(this.route.paramMap.subscribe( params => {const userId = params.get('userId');const userRef: AngularFirestoreDocument<用户>= this.db.doc(`users/${userId}`);userRef.valueChanges().subscribe(user => this.user = user);}))//html模板示例<div class="row justify-content-center"><div class="col-xs-12 col-sm-8 col-md-6 text-center name">{{user?.firstName}} {{user?.lastName}}
正如 Frank van Puffelen 所说,访问用户子集的最佳选择是
this.db.collection(`users/${userId}/saved`)
要访问您保存的子集合,您需要先访问用户集合中的用户文档.根据此 Firestore 文档,访问子集合的文档,您可以交替访问集合,然后访问文档,等等以到达子集合或子集合的文档.
<块引用>不能引用集合中的集合或文档中的文档.
为集合或子集合指定 this.db.collection ,为集合或子集合的文档指定 this.db.doc 很重要.
I have a sub-collection called saved as a nested collection to each user. User has its own fields and a collection of saved which contains two fields and an array. I can display only user fields but i don't know a proper way to access each user sub-collection.
export class SavedComponent implements OnInit{
public saved: Saved[];
public currentUser: any = null;
public user: User;
private subscriptions: Subscription[] = [];
constructor(
// Adding saved service
private savedService: SavedService,
private auth: AuthService,
private loadingService: LoadingService,
private route: ActivatedRoute,
private db: AngularFirestore
) {
// this.loadingService.isLoading.next(true);
}
this.subscriptions.push(
this.auth.currentUser.subscribe( user => {
this.currentUser = user;
this.loadingService.isLoading.next(false);
// Console log the current authentiucated user
console.log(this.currentUser);
})
);
this.subscriptions.push(
this.route.paramMap.subscribe( params => {
const userId = params.get('userId');
const userRef: AngularFirestoreDocument<User> = this.db.doc(`users/${userId}`);
userRef.valueChanges().subscribe(user => this.user = user);
})
)
// Example of html template
<div class="row justify-content-center">
<div class="col-xs-12 col-sm-8 col-md-6 text-center name">
{{user?.firstName}} {{user?.lastName}}
</div>
</div>
As Frank van Puffelen commented, your best bet to access a user's subcollection is
this.db.collection(`users/${userId}/saved`)
To reach your saved subcollection, you need to first access your user document inside your users collection. As per this Firestore document, a query's structure to access a subcollection's document, you alternate between accessing a collection, then a document, and so on to reach a subcollection, or a subcollection's document.
You cannot reference a collection in a collection or a document in a document.
It is important that you specify this.db.collection for a collection or subcollection, and this.db.doc for a collection's or subcollection's documents.
这篇关于使用 Angular firestore 返回 firebase 中的子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!