title: Node.js - Return Array from function
body: <p>I have a function that returns an array created with method <em>myArray.push({"key":value})</em></p>
  
  <p>In router file I did this, but it didn't work:</p>
  
  <pre><code>router.get('/:ID', function(req, res, next) {
      var myRenderArray = [];
      myRenderArray = auxFunctions.myArrayFunc(req.params.ID);
      res.render('myView', {title: 'title', myRenderArray});
  });
  </code></pre>
  
  <p>If I put the whole function inside router file, and change the return statement for <code>res.render('myView', {title: 'title', myRenderArray});</code> it works!</p>
  
  <p><strong>Resumed Function code:</strong></p>
  
  <pre><code>module.exports.myArrayFunc = function myArrayFunc(ID){
          var myArray = [];
          var id = req.params.ID;
          var req1 = new dbConfig1.Request(); 
          var req2 = new dbConfig2.Request();
  
  
          req1.query(query1('foo', ID)) 
          .then(function (array1) { 
                  req2.query(query2('foo', id, array1[0].fooId))         
                  .then(function (array2) {
                     req1.query(query3(array1[0].fooId))         
                      .then(function (array3) {
                          req1.query(query4('foo', ID, array1[0].fooId))      
                          .then(function (array4) {
                              myArray.push({
                                  'name1': key1,
                                  'name2': key2,
                                  'name3': key3,
                                  'name4': key4,                                
                              });
  
                              return myArray;
                          })
  
  
  
                          .catch(function (err) { console.log('****** Error on query 4'); console.log(err); });
                      })
                      .catch(function (err) { console.log('****** Error on query 3'); console.log(err); });
                  })
                  .catch(function (err) { console.log('****** Error on query 2'); console.log(err); });
          })
          .catch(function (err) { console.log('****** Error on query 1'); console.log(err); });
      }
  </code></pre>
  
  <p>How can I do?</p>
  
  <p>Thanks!</p>