【问题标题】:How to use 'Where' to read data from Firestore document in Google Cloud Functions?如何使用“在哪里”从 Google Cloud Functions 中的 Firestore 文档中读取数据?
【发布时间】:2021-11-10 22:38:33
【问题描述】:

我正在尝试使用 where 子句从我的谷歌云功能中查找我的 Firestore 数据库中的文档,这是我下面的代码,当我尝试获取带有 ID 的文档时,它可以工作,但是当我尝试使用where 子句没有,我该如何解决?我做错什么了吗

return await firestore.collection(COLLECTION_NAME).where('APIKEY', '==', '<APIKEY_VALUE>').get().then(doc => {            
  if (!(doc && doc.exists)) {
    return res.status(404).send({
      error: 'Unable to find the document'
    });
  }
  const data = doc.data();
  console.log(data);
  if (!data) {
    return res.status(404).send({
      error: 'Found document is empty'
    });
  }
  return res.status(200).send(data);
}).catch(err => {
  console.error(err);
  return res.status(404).send({
    error: 'Unable to retrieve the document',
    err
  });
});

这是我的 Firestore 数据库和我要收集的文档的屏幕截图:

【问题讨论】:

  • 运行这段代码时有什么问题?如果您希望查询与文档匹配,但事实并非如此,请编辑您的问题以包含您希望返回的文档的屏幕截图。
  • 我添加了截图,请告诉我我做错了什么
  • 谢谢。运行代码时会发生什么?有错误吗?如果没有,向客户端发送什么响应?
  • 您在查询中使用了firestore.collection(COLLECTION_NAME) 还是firestore.collection('accounts')
  • @FrankvanPuffelen 它给了我这个错误 { "error": "Unable to find the document" }

标签: javascript firebase google-cloud-platform google-cloud-firestore


【解决方案1】:

您应该将“COLLECTION_NAME”替换为集合的名称 你想找到。

firestore.collection(COLLECTION_NAME) 替换为firestore.collection('accounts')

【讨论】:

    猜你喜欢
    • 2021-03-16
    • 2018-07-28
    • 2021-03-29
    • 2021-04-18
    • 1970-01-01
    • 2018-03-27
    • 2020-03-07
    • 2021-04-13
    • 1970-01-01
    相关资源
    最近更新 更多