【问题标题】:Empty querySnapshot From the Parent collection父集合中的空 querySnapshot
【发布时间】:2020-08-11 16:34:02
【问题描述】:

有谁知道是否有一种方法可以在父集合的文档中进行迭代? 我尝试从我的父集合中获取每个文档,但 querySnapshot 似乎是空的,而事实并非如此。

我的收藏路径是这样的:

parentCollection(comments)/docs/subCollection(comments)/docs

我要做的是在每个子集合及其文档中进行迭代,为此我需要在父集合的每个文档中进行迭代,但是当我使用 get() 时,它返回一个空的 querySnapshot。

这是我的代码:

return admin.firestore().collection("comments").get().then((querySnapshot) => {
    console.log(querySnapshot);
    querySnapshot.forEach((doc) => {
      console.log(doc);
      return doc.ref.collection("comments").get().then((querySnapshot2) => {
        console.log(querySnapshot2);
        querySnapshot2.forEach((doc2) => {
          console.log(doc2.data());
        })
        return true;
      });
    });
    return true;
  });

Ps:我知道,我不应该嵌套 promise。

更新

当记录querySnapshot.sizequerySnapshot.empty 时,我的console.log 返回的内容如下:

这里是整个代码:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();


exports.onUpdateUserUpdateComments = functions.firestore
    .document("/users/{userId}")
    .onUpdate(async (change, context) => {
      const userId = context.params.userId;
      const userUpdate = change.after.data();
      const newPhotoUrl = userUpdate.photoUrl;
      console.log("userId",userId);
      console.log("newPhotoUrl",newPhotoUrl);
      return admin.firestore().collection("comments").get().then((querySnapshot) => {
        console.log(querySnapshot.size);
        console.log(querySnapshot.empty);
        querySnapshot.forEach((doc) => {
          console.log(doc);
          return doc.ref.collection("comments").get().then((querySnapshot2) => {
            console.log(querySnapshot2);
            querySnapshot2.forEach((doc2) => {
              console.log(doc2.data());
            })
            return true;
          });
        });
        return true;
      });
 });

在这里部署后的日志输出

【问题讨论】:

  • 是说在admin.firestore().collection("comments").get().then((querySnapshot) => {之后,querySnapshot没有结果?你能console.log(querySnapshot.size) 更新它的输出吗?
  • @FrankvanPuffelen 是的,就是这样,而我的 cmets 集合甚至不是空的......我注意到,当我试图只获取具有子集合的父集合的文档时,总是会发生这种情况。
  • 我不知道这种情况的已知问题。从您迄今为止共享的代码/数据中很难看出发生了什么。您能否尝试在我们任何人都可以在本地运行的单个 Node.js/JavaScript sn-p 中重现该问题?
  • 我已经在本地复制了。我在我的 Firestore 中创建了相同的结构,并将您的代码完全放入函数中,并且我刚刚使用 node 调用它并且它工作正常,您在这个嵌套集合中有什么吗?我认为问题可能在于您如何运行它...也许您可以添加代码更多代码....
  • @vitooh 你能告诉我你的代码吗?也许是因为我错过了一些东西,也许我没有初始化一些东西..

标签: javascript node.js google-cloud-firestore google-cloud-functions


【解决方案1】:

我已经实现了这个功能,完全复制/粘贴你的代码,我可以确认它工作正常。

当然,我还将结构添加到我的测试 Firestore:

-comments - <genrated docID1> -comments - empty
          - <genrated docID2> -comments - <dummy document> 

users 结构以及使触发器工作。我通过手动更新“photoUrl”字段来触发它。

有了这个结构,querySnapshot.size 给出了2 的值,而querySnapshot.emptyfalse

确实,您可以在日志QuerySnapshot_size: 0, 中找到,但是这是针对这个嵌套的空“cmets”集合的。

顺便说一句,此代码父集合QuerySnapshot 中根本没有记录。只有父集合和嵌套集合的文档。

我希望这会有所帮助!

【讨论】:

    【解决方案2】:

    经过多次测试,我可以看出问题是由于即使从控制台可以看到文档,它们也不存在。 它们不存在,因为即使它们包含子集合,它们也不计为数据。所以这就是为什么文档是斜体,为什么下一句是显示:“此文档不存在,不会出现在查询或快照中

    要解决此问题,您必须将数据添加到这些文档中并在不需要时将其删除,或者您可以在控制台中手动创建它们。 或者更好的是,如果不想更改代码中的任何内容,可以使用 listDocuments 方法。 Admin SDK 提供了一个带有以下描述的 listDocuments 方法:

    返回的文档引用可能包括对“缺失文档”的引用,即不存在文档但包含带有文档的子集合的文档位置。尝试读取此类文档引用(例如,通过 .get() 或 .onSnapshot())将返回 .exists 属性为 false 的 DocumentSnapshot。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-26
      • 2022-01-18
      • 1970-01-01
      • 2020-10-24
      • 2022-11-19
      • 2022-01-05
      • 2020-12-19
      • 1970-01-01
      相关资源
      最近更新 更多