I have a function that returns an array created with method myArray.push({"key":value})
In router file I did this, but it didn't work:
router.get('/:ID', function(req, res, next) {
var myRenderArray = [];
myRenderArray = auxFunctions.myArrayFunc(req.params.ID);
res.render('myView', {title: 'title', myRenderArray});
});
If I put the whole function inside router file, and change the return statement for res.render('myView', {title: 'title', myRenderArray}); it works!
Resumed Function 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); });
}
How can I do?
Thanks!