我有一個附加到子集合的快照偵聽器(此子集合記錄對父檔案的所有更改)。對于此子集合中的每個檔案,我想檢索父檔案。我應該在下面的代碼中用什么替換 XXXXXXXXXXX?
const unsubToProfilesRecentlyUpdatedByUser = onSnapshot(qChangedByThisUser, (querySnapshot) => {
store.state.currentProfileList = [];
querySnapshot.forEach((doc) => {
const parentProfileRef = doc(db, "profiles", XXXXXXXXXXX);
//Get the parent Profile of this Activitylog
getDoc(parentProfileRef)
.then((profileSnap) => {
//store resulting profile in object
let profile = profileSnap.data()
profile.id = profileSnap.id
store.state.currentProfileList.push(profile)
})
});
//console.log("Current cities in CA: ", cities.join(", "));
});
我已經嘗試了其他帖子中的許多示例,并且在僅收聽更改時設法使其作業,在這種情況下
const parentProfileRef = doc(db, "profiles", change.doc.ref.parent.parent.id);
但是當像上面那樣聽整個狀態時,我不能做的是作業。
感謝您的任何提示!
uj5u.com熱心網友回復:
如果doc
是DocumentSnapshot物件,那么您可以像這樣獲取其父物件的DocumentReference :
const parentRef = doc.ref.parent.parent;
然后,您可以將其傳遞給getDoc()
. 無需使用 構建另一個 DocumentReference doc()
。
這適用于所有 DocumentSnapshot 物件。它對您用于獲取 DocumentReference 的方法并不特殊。請務必使用鏈接的 API 參考來了解如何使用從 Firestore API 回傳的各種物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507505.html