首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firestore如何获取单据列表中的顶级集合?

Firestore如何获取单据列表中的顶级集合?
EN

Stack Overflow用户
提问于 2021-04-19 03:14:38
回答 1查看 27关注 0票数 0

example1)集合-单据-集合-单据-集合

example2)集合

example1结构,我可以导入最后一个集合中的所有文档列表。

example2结构,我无法获取集合中的所有文档列表。

如何获取单据列表中的顶级集合?

下面是我的代码。

代码语言:javascript
复制
// 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返回空数组。为什么会这样呢?

EN

回答 1

Stack Overflow用户

发布于 2021-04-19 04:49:27

从Firestore加载数据很浅。这意味着,如果从users集合加载文档,则不会自动包含子集合中的数据。

如果您希望从特定用户的video子集合加载数据,则需要进行额外的调用:

代码语言:javascript
复制
  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());
        });
      }
    });
  });

如果你想加载所有用户的所有视频,你可以使用所谓的收集组查询:

代码语言:javascript
复制
  dbService
   .collectionGroup("videos")
   .onSnapshot((snapshot) => {
    snapshot.docs.map((doc, index) => {
      videoList.push(doc.data());
      console.log(doc.data());
    });
  });

如果您想在这里找到某个特定视频的用户ID,可以通过doc.ref.parent.parent.id找到。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67152474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档