微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

谷歌云功能删除“丢失文件”的子集合

如何解决谷歌云功能删除“丢失文件”的子集合

我有一个名为 Posts 的 Firestore 集合,Posts 中的每个文档都可以有一个名为 Post-Likes 和/或 Post-Comments 的子集合。当我删除 Posts 文档时,它不会删除子集合,所以我只剩下对 Firestore 中缺少的文档的引用,如下所示:

enter image description here

我在 Google Cloud Function 中使用以下代码在 Post 集合中查找缺少数据的引用,然后对于每个缺少引用的文档,我想删除子集合 Post-Likes 和 Post-Comments。现在,我只是尝试列出子集合文档,以便我可以删除它们,但出现错误

function deleteOrphanPostSubCollections() {

    let collectionRef = db.collection('Posts');

    return collectionRef.listDocuments().then(documentRefs => {
       return db.getAll(...documentRefs);
    }).then(documentSnapshots => {
       for (let documentSnapshot of documentSnapshots) {
          if (documentSnapshot.exists) {
            console.log(`Found document with data: ${documentSnapshot.id}`);
          } else {
            console.log(`Found missing document: ${documentSnapshot.id}`);
            return documentSnapshot.getCollections().then(collections => {
              return collections.forEach(collection => {
                console.log('Found subcollection with id:',collection.id);
              });
            });
          }
       }
       return
    });
}

但是,我收到以下错误。请帮我解决这个问题。

enter image description here

解决方法

这是因为 DocumentSnapshot 没有 getCollections() 方法。

如果要列出DocumentSnapshot对应的Document的所有集合,需要使用listCollections()方法,如下:

documentSnapshot.ref.listCollections()
  .then(collections => {
    for (let collection of collections) {
      console.log(`Found subcollection with id: ${collection.id}`);
    }
  });

另外,请注意,如果您在循环中调用异步方法,建议使用 Promise.all() 以便在所有“输入”Promise 都解析后返回一个解析的 Promise。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?