【问题标题】:Making HTTPS Request with Node.JS to Firebase使用 Node.JS 向 Firebase 发出 HTTPS 请求
【发布时间】:2020-02-02 05:55:54
【问题描述】:

我正在尝试使用 node.js 在 firestore 中收集所有文档,但我无法获取任何文档,尽管有 有人知道这个函数有什么问题:

exports.getTasksNew8 = functions.https.onRequest((request,response) => {
    admin.firestore().collection('users/admin/settings').get().then(querySnapshot=>{
        querySnapshot.forEach(documentSnapshot=>{
            console.log(documentSnapshot.id)
        })
    }).catch(error=>{
    console.log(error)
    })
    response.send('works ok)
})

【问题讨论】:

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


    【解决方案1】:

    get() 方法是异步的并返回一个 Promise:您应该在 then() 方法中“处理”结果。见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/thenWhat does the function then() mean in JavaScript?还有https://scotch.io/tutorials/javascript-promises-for-dummies#toc-promises-are-asynchronous

    通过在then() 之外执行response.send('works ok),您向云函数表明它可以在异步get() 完成之前完成。

    修改你的代码如下:

    exports.getTasksNew8 = functions.https.onRequest((request,response) => {
        admin.firestore().collection('users/admin/settings').get().then(querySnapshot=>{
            querySnapshot.forEach(documentSnapshot=>{
                console.log(documentSnapshot.id)
            })
            response.send('works ok);
        }).catch(error=>{
          console.log(error);
          //See the following video https://firebase.google.com/docs/functions/video-series#learn-javascript-promises-pt1-with-http-triggers-in-cloud-functions
        })
    
    })
    

    【讨论】:

      猜你喜欢
      • 2014-04-16
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多