Cloud Firestore Function仅返回一个文档?

时间:2018-11-12 06:09:24

标签: firebase google-cloud-firestore google-cloud-functions

我创建了一个firebase函数来返回帖子集合,出于某种原因,cloud函数仅返回一个帖子(文档)。但是当我在本地运行此功能时,在终端中我会收到所有帖子。知道为什么吗?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require('cors')({origin: true});
admin.initializeApp();

exports.postWidget = functions.https.onRequest((request, response) => {
cors(request, response, () => {});

var db = admin.firestore(); 

var postsRef = db.collection('posts')
postsRef.where('postStatus', '==', 'published').get()
.then(snapshot => {
    snapshot.forEach(doc => {
        let data = doc.data()
        return response.send(data)
    });
})
.catch(err => {
    console.log('Error getting documents', err);
});

});

1 个答案:

答案 0 :(得分:0)

您在forEach循环中多次调用// Load pdf library $this->load->library('pdf'); // Get output html $html = '<html lang="en"> <head> <title>Certificate</title> </head> <body> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td align="center" style="padding-bottom: 30px;"> <img src="http://dev.rglabs.net/recharge/images/logo.jpg" alt="logo" style="width: 186px; height: 146px;"/> </td> </tr> <tr> <td> <p style="font-size: 28px;">Certificate of authorised dealership</p> </td> </tr> </table> </body> </html>'; // Load HTML content $out=$this->dompdf->loadHtml($html); // (Optional) Setup the paper size and orientation $this->dompdf->setPaper('A4', 'landscape'); $this->dompdf->set_option('isRemoteEnabled', TRUE); // Render the HTML as PDF $this->dompdf->render(); $pdf_string = $this->dompdf->output(); // Output the generated PDF (1 = download and 0 = preview) $this->dompdf->stream("welcome.pdf", array("Attachment"=>0)); die; 。这不太可能做您想做的事情,因为您只应该在整个响应后一次调用response.send(data)。相反,您应该收集所有数据以发送到单个对象中,并使用该对象调用send()