I have to fill my risultato json with some information taken by MongoDB with a mongoose query, so at the end of the loop i have to return risultato with an hapijs controller. If i use a return at the end i have an error like this:
Error:reply interface called twice
And if i use the return out of the loop it return:
var risultato = {
"models": [],
}
This is my code:
Myfunction: function(request, reply) {
var risultato = {
"models": [],
}
for(var i=0; i<3; i++){
Collections //this is a mongoose model
.findOne()
.where({_id: id]})
.populate('models')
.exec( function(err, result) {
risultato.models.push.apply(risultato, result.models);
console.log(risultato)
return reply(risultato)
});
}
};
What can i do?