I've got two .js files on the server-side: app.js and manageDB.js. I would like to retrieve data from MongoDB and use it on the client-side.
manageDB.js:
exports.getClients = function() {
db.clients.find(function(err, docs) {
if (err) {
console.log(err);
} else {
return docs;
}
});
};
And I would like to do something like this in app.js:
app.get('/', function(req, res) {
var clients = manageDB.getClients();
res.render('index.html', {myClients: clients});
});
but clients are undefined because of asynchronism. If I log docs in the console within getClients function (manageDB.js file) it is ok but if I try to console.log(manageDB.getClients()) within app.js file - it doesn't work. I would appreciate any help.