获取我在Cloud Firestore中所有文档的对象 [英] get object for all my documents in Cloud Firestore
本文介绍了获取我在Cloud Firestore中所有文档的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从Express创建的后端发送Firestore中所有文档的JSON
I want to send a JSON of all my docuements in Firestore from my backend created with express
Firestore的文档说明了如何获取所有文档,但是该方法使用 forEach
,并且express只能发送一次响应.所以问题是我不知道如何将所有forEach呈现为一个变量,以便为标头发送一次
The Firestore docs says how to get all documents but the method is with forEach
and express can send only once the response. So the problem is that I don't know how to render all forEach in a variable to send once for the headers
这是Firestore文档的代码:
This is the code of Firestore docs:
db.collection('users').get()
.then((snapshot) => {
snapshot.forEach((doc) => {
console.log(doc.id, '=>', doc.data());
});
})
.catch((err) => {
console.log('Error getting documents', err);
});
我希望有人能帮助我.
推荐答案
You could for example create and populate a JavaScript object of type array:
db.collection('users').get()
.then((snapshot) => {
var usersArray = [];
snapshot.forEach((doc) => {
console.log(doc.id, '=>', doc.data());
usersArray.push(doc.data());
});
//do something with the usersArray
//e.g. return usersArray;
///or return JSON.stringify(usersArray)
})
.catch((err) => {
console.log('Error getting documents', err);
});
这篇关于获取我在Cloud Firestore中所有文档的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文