【问题标题】:Getting an empty query when querying documents on Firebase Firestore在 Firebase Firestore 上查询文档时获取空查​​询
【发布时间】:2020-12-29 00:52:09
【问题描述】:

我想构建一个 Firebase 函数,用于检查“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());
    
    });

  });

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2020-11-22
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多