获取我在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);
    });