title: how to export array created in a promise
body: <p>I'm trying to follow a project from <a href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Tutorial_local_library_website" rel="nofollow noreferrer">mozilla developer local library tutorial</a>
  . It includes learning how to use nodejs, express, mongoose and mongoDB. I am swapping out mongoose and mongoDB with firestore and using firebase functions to eventually host with firebase hosting.</p>
  
  <p>The project uses a model, view, controller structure. I really want to stick to this structure to help break everything down into manageable chunks.</p>
  
  <p>The first problem I’m trying to resolve is to return an array that I created in a promise to be exported as a function that is called from booksController.js.</p>
  
  <p>A concise view of the structure I have is:
  index.js</p>
  
  <pre><code>const booksRouter = require('./routes/books');
  app.use('/books', booksRouter);
  </code></pre>
  
  <p>routes/books.js</p>
  
  <pre><code>const books_controller = require('../controllers/booksController');
  router.get('/', books_controller.index);
  </code></pre>
  
  <p>controllers/booksController.js</p>
  
  <pre><code>var Books = require('..models/books');
  exports.index = function(req, res){
      var books = Books.AllBooks();
      res.render('books',{books: books});
  };
  </code></pre>
  
  <p>models/books.js</p>
  
  <pre><code>var booksRef = db.collection(books);
  var allBooks = booksRef.get()
      .then(snapshot =&gt; {
          var books = [];
          Snapshot.forEach(doc =&gt; {
              books.push({doc.data: doc.data()});
          });
  
      });
  .catch(err =&gt;{
      console.log('Error getting documents', err);
  });
  exports.AllBooks = function(req, res){
      return books;
  }
  </code></pre>
  
  <p>I've tried wrapping the promise in a function and returning the array to export it but I get undefined when console logging in booksController.js.</p>
  
  <p>Can someone please enlighten me and complete the puzzle of how to return the array so it can be exported.    </p>