BradCoffield
11/22/2019 - 5:56 PM

Async/Await Firestore Between Modules

 let dataAccess = require("../db");
 
 (async () => {
      try {
        let rez = await dataAccess.getFirestoreCollection(theCollection); 
        res.send(rez);
      } catch (err) {
        console.log("ERROR:", err);
      }
    })();

module.exports = function getValues(theCollection) {
  console.log("start");
  var result;

  return db //note this return________
    .collection(theCollection)
    .get()
    .then(snapshot => {
      let theData = [];
      snapshot.forEach(doc => {
        theData.push({ [doc.id]: doc.data() }); 
      });
      result = theData;
      console.log("HERE WE BE");
      return result; //note this return__________
      })
    .catch(err => {
      console.log("Error getting documents", err);
    });
}