简体   繁体   English

使用 Node.JS 向 Firebase 发出 HTTPS 请求

[英]Making HTTPS Request with Node.JS to Firebase

i am trying to get all docs in collection in firestore with node.js but i can't get any docs althow there is someone know what is the problem in this function:我正在尝试使用 node.js 在 Firestore 中收集所有文档,但我无法获得任何文档,尽管有人知道这个 function 有什么问题:

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)
})

The get() method is asynchronous and returns a Promise: you should "treat" the result within the then() method. get()方法是异步的,并返回 Promise:您应该在then()方法中“处理”结果。 See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then and What does the function then() mean in JavaScript?请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/thenfunction then() 的含义是什么? and also https://scotch.io/tutorials/javascript-promises-for-dummies#toc-promises-are-asynchronous还有https://scotch.io/tutorials/javascript-promises-for-dummies#toc-promises-are-asynchronous

By doing response.send('works ok) outside the then() you indicate to the Cloud Function that it can finish before the asynchronous get() is done.通过在then() response.send('works ok) ,您可以向 Cloud Function 表明它可以在异步get()完成之前完成。

Modify your code as follows:修改您的代码如下:

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
    })

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM