What is the difference in these calls, why in first case my console logs 'undefined', and in second I get the right answer (an integer).
When calling count() - undefined.
function count() {
let answer;
Url.findOne({}).sort({short_url:-1}).exec(function (err,ur) { if (err) return err; answer = ur.short_url });
console.log(answer);
}
When calling count() - an integer.
function count() {
Url.findOne({}).sort({short_url:-1}).exec(function (err,ur) { if (err) return err; console.log(ur.short_url) });
}
In general, I want to achieve that such a call (count()) would return highest value of 'short_url'.