【问题标题】:Firestore query not workingFirestore 查询不起作用
【发布时间】:2017-11-26 04:54:49
【问题描述】:

上面显示的是我的 Firestore 收藏。

我正在尝试使用我已部署的 Google Cloud 函数从该集合中获取数据:

const admin = require('firebase-admin')
const functions = require('firebase-functions')

module.exports=  function(request, response){ 
     let results = []   
     admin.firestore().collection('news_stories')
     .get()
     .then(docs => docs.map(doc => results.push(doc.data())))
     .catch(e => resoponse.status(400).send(e))

      response.status(200).send(results)
}

当我运行上述函数时,我得到一个:

错误:无法处理请求

我也尝试过以这种方式运行该函数,看看它是否可以工作。

  module.exports=  function(request, response){ 
     let ref = admin.firestore().collection('news_stories')
     .get()
     .then(docs =>   response.status(200).send(docs))
     .catch(e => resoponse.status(400).send(e))
}

这个函数返回了一个这个 JSON 对象:

没有关于数据或任何文档的信息。

我使用此功能将集合上传到了 firestore 数据库:

const functions = require('firebase-functions')
const admin = require('firebase-admin')

module.exports = function(request,response){
   if(!request.body.data){
      response.status(422).send({error: 'missing data'})
   }
   let data = request.body.data
   data.map(item => { 
      admin.firestore().collection('news_stories').add({item})
      })
     response.status(200).send('success!')
}

不知道我做错了什么。为什么函数没有返回任何文档?

【问题讨论】:

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


    【解决方案1】:

    从 Firestore 异步检索数据。当您将响应发送回给调用者时,尚未从 Firestore 检索结果。

    用三个日志语句替换大部分代码最容易看到这一点:

    console.log("Before starting get");
    admin.firestore().collection('news_stories')
      .get()
      .then(() => {
        console.log("In then()");
      });
    console.log("After starting get");
    

    最好在常规 node.js 命令中运行上述命令,而不是在 Cloud Functions 环境中,因为后者实际上可能会在加载数据之前终止命令。

    上面的输出是:

    开始获取之前

    开始get后

    在then()中

    这可能不是您期望的顺序。但由于数据是从 Firestore 异步加载的,所以回调函数之后的代码可以直接继续执行。然后,当数据从 Firestore 返回时,您的回调将被调用,并且可以根据需要使用数据。

    解决方案是将所有需要数据的代码移入then()处理程序:

    const admin = require('firebase-admin')
    const functions = require('firebase-functions')
    
    module.exports=  function(request, response){ 
         admin.firestore().collection('news_stories')
         .get()
         .then(docs => {
           let results = []   
           docs.map(doc => results.push(doc.data()))
           response.status(200).send(results)
         })
         .catch(e => resoponse.status(400).send(e))
    }
    

    【讨论】:

    • 感谢您的时间和帮助,我已经对代码进行了编辑,不幸的是它仍然无法正常工作,无论是在 Node.js 环境中还是在云中......我一直在获取状态500 错误代码 ...
    • 这肯定是您现有代码中的错误。可能还有更多。
    • 我让云函数工作......错误的根源在 .map 函数中......一旦我将 .map 更改为 forEach 函数运行并返回数据
    • 很高兴听到。我实际上想知道 Firestore 的快照是否实现了映射,因为 Firebase 实时数据库的快照没有。我想你已经找到了答案。 :-)
    【解决方案2】:

    所以经过一些故障排除后,我找到了问题的根源。出于某种原因,如果您在返回对象上使用 .map,服务器将以 500 状态响应... 将 .map 更改为 forEach 并且该功能有效

    这会起作用的......

      admin.firestore().collection('news_stories')
        .get()
        .then(docs => {
            let data = []
            docs.forEach(doc => data.push(doc.data()))
           response.status(200).send(data)
      })
    

    但这不会...

        admin.firestore().collection('news_stories')
          .get()
           .then(docs => {
             let data = []
             docs.map(doc => data.push(doc.data()))
             response.status(200).send(data)
         })
    

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 2018-03-18
      • 2020-07-27
      • 2019-02-05
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多