I'm trying to run a for loop over a mongo query to continuously retrieve data from the database. After it's retrieved I then try and push it to an array then res.send(array)
to avoid the unhandled promise rejection. But instead, the data isn't being pushed into the array.
router.get('/results/:week',function(req,res,next){
var week = req.params.week - 1;
var results = [];
for(week; week>0; week--){
Fixture.find({'week':week}).then(function(fixture){
results.push(fixture);
});
}
res.send(results);
The Fixture model contains a variable week, and I want to retrieve all the results(fixtures) from a certain week downwards. The result given is and empty array []
and if I res.send(fixture)
I get the unhandled promise rejection warning. Please help and thank you.