在Firebase Firestore上查询文档时获取空查询

罗恩

我想建立一个Firebase Function,检查“ Friends”集合中的“ scheduled,==,true”,然后在“ Filings”集合中使用“ fname”创建一个新文档。

我的Firetore设置如下:

 Users (collection)
    User IDs (documents)
         |
         |
  Friends (collection)
     Friends (documents with auto IDs)
     [fname, scheduled: true/false]
            |
            |
         Filings (collection)
            Filings (documents with auto IDs)  

这是屏幕截图:

每当我运行以下函数时,都会收到一条错误消息,指出该文档为空,即使不是。我不明白为什么会这样。希望得到您的建议

exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun(async context => {
    const task = await (db.collection('friends').where('scheduled', '==', true).get());

    if (task.empty) {

        console.log('doc is empty');
        return;
    }

    task.forEach(element => {
        console.log(element.id, '==>' , element.data());
    
    });

  });
弗兰克·范普菲伦

您的friends收藏集嵌套在users文档下。您的db.collection('friends')代码仅访问顶级friends集合,而顶级集合可能不存在。

要访问所有friends集合,请使用:

db.collectionGroup('friends').where('scheduled', '==', true).get()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章