I'm using Node and express and ejs for a simple apple to test with. I have the following piece of code which is triggered by a route call to my results page. What I want is to redirect to the results page after fetching all the results from my mongodb collection. Everything works fine and in my console.log in the GetResults function in my db file the results are being retrieved and returned by the function. I think I'm dealing with the async nature of my call to DBHandler, but how can I accomplish this?
router.get("/results",isLoggedIn, function(req,res){
var results = DBhandler.GetResults(req,res);
console.log("value of results right before it is passed to results.ejs: ", results)
////COME BACK AND MAKE THIS A PROMISE TO RUN AFTER THE RESULTS IS FETCHED
res.render("results.ejs", {results:results});
});
My DB function
exports.GetResults = function (req, res) {
console.log(req.body);
//get all the results
ResultModel.find({},function(err, result){
if(err){
console.log("SOMETHING WENT WRONG GETTING RESULTS: ", err)
}else{
console.log("this is the value of result from the getresults call: ", result);
return result;
}
});
}