example1)集合-单据-集合-单据-集合
example2)集合
example1结构,我可以导入最后一个集合中的所有文档列表。
example2结构,我无法获取集合中的所有文档列表。
如何获取单据列表中的顶级集合?
下面是我的代码。
// this is example1. this is working!!
dbService
.collection("users")
.doc(uid)
.collection(uid)
.doc("video")
.collection(uid)
.onSnapshot((snapshot) => {
snapshot.docs.map((doc, index) => {
videoList.push(doc.data());
console.log(doc.data());
});
});
// this is example2. this is not working !!!!!
dbService
.collection("users")
.onSnapshot((snapshot) => {
snapshot.docs.map((doc, index) => {
videoList.push(doc.data());
console.log(doc.data());
});
});example2返回空数组。为什么会这样呢?
发布于 2021-04-19 04:49:27
从Firestore加载数据很浅。这意味着,如果从users集合加载文档,则不会自动包含子集合中的数据。
如果您希望从特定用户的video子集合加载数据,则需要进行额外的调用:
dbService
.collection("users")
.onSnapshot((snapshot) => {
snapshot.docs.map((doc, index) => {
if (/* this is a user you are interested in */) {
snapshot.ref.collection(videos).get().then((videos) => {
videos.forEach((video) => {
videoList.push(video.data());
})
console.log(doc.data());
});
}
});
});如果你想加载所有用户的所有视频,你可以使用所谓的收集组查询:
dbService
.collectionGroup("videos")
.onSnapshot((snapshot) => {
snapshot.docs.map((doc, index) => {
videoList.push(doc.data());
console.log(doc.data());
});
});如果您想在这里找到某个特定视频的用户ID,可以通过doc.ref.parent.parent.id找到。
https://stackoverflow.com/questions/67152474
复制相似问题