I am trying to implement a function in Express to return a list with data from a mongoose model. 'MiModelo' is the mongoose model created from a Schema.
//Get data in the DB
function getAllData()
{
var promesa = MiModelo.find().exec();
console.log(promesa);
console.log("---");
var miLista=[];
async = require('async');
async.parallel([function(){
promesa.then(function(datos)
{
datos.forEach(function(dato){
console.log("dato: " + dato.numero)
miLista.push(dato.numero);
});
});
}],function(){
console.log(miLista);
});
return miLista;
}
In the final console.log() I am able to get all 'numero' field values from the database but the return is empty when I call this function elsewhere. I know it is because it is asynchronous.
I have read the answer in this question: How to make a function wait until a callback has been called using node.js but I do not know how to adapt my function.
Any help is appreciated.
Thank you for your time and help.