I have a function which gets all record in my collection in mongodb.
var getVouchers= function(){
Customer.find({}, function(err, result) {
if (err) throw err;
return result;
});
};
I call like this and when I put break point, I can see that I have result. The problem is, when I call this function, it does not wait..
As it is nodejs, I exported the function as follow
module.exports.getVouchers =getVouchers;
and I call like this
var router = require('./api');
adminPanel.get('/', (req,res)=>
{
router.getVouchers().then((result)=>{
console.log(result);
});
});
and I got the following exception
(node:26972) [DEP0016] DeprecationWarning: 'GLOBAL' is deprecated, use 'global'
event I do not use GLOBAL
keyword
how to solve this problem?
edit:
I updated the my code now
var getVouchers = function() {
// `delay` returns a promise
return new Promise(function(resolve, reject) {
Customer.find({}, function(err, result) {
if (err) throw err;
return result;
});
});
}
and I call the function as
(async function(){
let vouchers=await router.getVouchers();
console.log(vouchers);
})();
the first function has the result but there is no return value, that means, it never comes to the console.log part.
Edit 2:
Instead of return result, as @ajuni880 said, I made resolve(result); and it works now.