0

I want to render the result of an aggregated data from MongoDB and be able to render it in Browser in the table using Handlebars Template. Sorry I am new to Handlebars

Below is my code:

var leastArticleRevision = [
        {'$group':{'_id':"$title", 'numOfEdits': {$sum:1}}},
        {'$sort':{numOfEdits:1}},
        {'$limit':5}    
    ];

var test = Revision.aggregate(leastArticleRevision, function(err, results){
    if (err){
        console.log("Aggregation Error")
    }else{
        console.log("The least editted articles are: ");
        console.log(results)
    }
})


router.get('/', function(req, res){
    res.render('index',{
        content: "Hello World",
        test: test
    });
});
PONCY
  • 11
  • 1
  • 6
  • Removing tags does not change the context of the question. `var test = Revision.aggregate` does not return the value to `test`. That is what the answer on the linked question is showing you. Also the function in not in the scope of the route handler. You should read the answer and learn from it so you can apply what you learn. – Neil Lunn May 20 '18 at 03:14
  • What you "need" to do is understand how asynchronous calls and variable scoping work in JavaScript "first". That is why the question is placed on hold as a duplicate of that very issue. If you have "no idea why", then I suggest you look at the very prominent link sitting above your question. – Neil Lunn May 20 '18 at 03:25

0 Answers0