So Ive seen a lot of similar questions about counting database entries and I have figured out how to use callback functions to console.log the number, but because of the local scope of that callback I cant figure out how to actually USE that number in another function. I would like to make a random number generator for between 0 and the number of documents in my database. This is what I have so far.
Comic.countDocuments({}, function (err, count) {
if (err){
console.log(err)
}else{
console.log(count)
}
});
I would love for it to work like this but it doesnt. Whenever I do it num ends up being a huge amount of metadata instead of a number. Any ideas?
let num = Comic.countDocuments({}, function (err, count) {
if (err){
console.log(err)
}else{
return count
}
});
console.log(Math.floor(Math.random() * num))