Following is my code:
Middleware:
app.get('/myapp', function (req, res) {
db.collection('mydocument').find(function (err, docs) {
var result = checkAuthorisedUser(); //calling function checkAuthorisedUser from here
console.log("result:" + result);
if (docs.length > 0)
res.json(docs);
else
res.json(null);
});
});
Function checkAuthorisedUser()
function checkAuthorisedUser() {
var result;
db.collection('user').find({uid: session.uid}, function (err, docs) {
console.log(docs); // Here its displaying result
result = docs;
});
return result;
}
In above code I'm trying to call a function from middleware where I've written mongodb query to find userid
but its not assigning docs
to var result
so that's why it's not returning the data and showing undefined
in the console.log(result)
.
I'm beginner in express.js
. Any help would be appreciated.