I have declared variable at global level in function, which eventually get changed within inner function and I want to return changed variable value as outer function's value but currently getting undefined.Plz provide guidance.
function checkResult(req){
let result = true;
Reservation.find({result_date: req.body.res_date}, function (err,doc) {
if (err) {console.log(err);}
else if (reservations) {
result = false;
console.log(result);
}
})
console.log("Final:");
return result; // undefined error
}
You should use callback.
For example:
function checkResult(req, callback){
let result = true;
Reservation.find({result_date: req.body.res_date}, function (err,doc) {
if (err) {console.log(err);}
else if (reservations) {
result = false;
}
callback(result);
})
}
And then use the function like this:
checkResult(req, function(result){
console.log(result); // prints the boolean
});