gpt4 book ai didi

javascript - 在子集合中查询?

转载 作者:行者123 更新时间:2023-12-02 22:34:42 25 4
gpt4 key购买 nike

这是我的结构: enter image description here enter image description here

我想查询并显示特定用户所在的所有组。

我正在像这样查询用户:

 componentDidMount() {
const uid = auth().currentUser.uid;
firestore()
.collectionGroup('Members')
.where('uid', '==', `${uid}`)
.get()
.then(doc => {
doc.forEach(snap => {
console.log(snap.id); // Gives User Document under Members sub-collection
});
});
}

但是如何检索用户所在文档的 groupdId(从“Groups”父集合)?

最佳答案

在您的代码中,snapDocumentSnapshot类型对象,其中包含对在其 ref 中查询的文档的完整路径的引用属性(property)。您可以使用这个DocumentReference对象通过使用其 parent 沿着路径向上查找父集合和文档属性,或通过解析其 path字符串。

// a CollectionReference to Groups/{groupId}/Members
snap.ref.parent

// a DocumentReference to Groups/{groupId}
snap.ref.parent.parent

// the string ID of the above DocumentReference {groupId}
snap.ref.parent.parent.id

关于javascript - 在子集合中查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58777414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com