I am trying to return data from this function. Console.log(documents) successfully shows the data in console. But this works only in body of the function. I can't return this data to the template. What should I do? Should I use some async package for node.js, or can be accomplished somehow like this?
Thank you.
var projects = req.user.projects;
var docs = [];
db.collection('documents', function(err, collection) {
    for (i = 0; i < projects.length; i++) { 
        collection.find({'_projectDn': projects[i].dn},function(err, cursor) {
            cursor.each(function(err, documents) {
                if(documents != null){
                    console.log(documents);
                    //or docs += documents;
                }
            });
        });
    }
});
console.log(documents); // undefined
res.render('projects.handlebars', {
    user : req.user,
    documents: docs
});
 
     
    