![](/img/trans.png)
[英]How to return parent collection based on subcollection document value in Firestore?
[英]How to find the parent collection to a document in Firebase Firestore?
我在 Firebase 中搜索一個子查詢,我試圖找到它所屬的父集合(然后是父文檔)。 這是我的代碼
let queryRef = orgRef.where('User_ID', '==', userID).get()
.then(snapshot => {
if (snapshot.empty) {
console.log('No matching documents.');
return;
}
snapshot.forEach(doc => {
console.log(doc.parent());
});
})
.catch(err => {
console.log('Error getting documents', err);
});
但是使用 doc.parent 或 snapshot.parent 不起作用。 如何找到子集合的父文檔?
如果您有 DocumentSnapshot object,您將需要使用其ref
屬性來獲取指向它的DocumentReference object。 DocumentReference 有一個parent
屬性,它將為您提供文檔所在的集合。 所以,你可能會想要:
doc.ref.parent
對於查詢結果中的任何給定doc
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.