问题
I want to send a JSON of all my docuements in Firestore from my backend created with express
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
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);
});
I hope somebody can help me.
回答1:
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);
});
来源:https://stackoverflow.com/questions/56322010/get-object-for-all-my-documents-in-cloud-firestore