I am having some issues with block scoping in Node. I am attempting to build a object stuffed with some stats from the database. The issue I am running into is the object gets defined just fine inside of a mongoose db call, but is undefined outside of it.
I have also tried doing it as an array and doing stats.push(), and both ways with a setTimeout (set to 300ms). Any suggestions? I could do a method on the model I supposed, but I would rather learn about what i'm doing wrong here.
app.get('/api/v1/authenticated/admin/stats/get', function(req, res) {
let stats = {};
User.find().count(function(err, count){
stats.userCount = count;
console.log('usrCnt inside of db ' + stats.userCount); //defined. Shows in the console after the console.log below.
});
console.log('UsrCount outside of db ' + stats.userCount); //undefined
res.send(stats);
});