UPDATE
After some testing around, this works:
Url.count({}, (err, data) => {
console.log(data); // prints correct number
});
But this doesn't:
let len;
Url.count({}, (err, data) => {
len = data;
});
console.log(len); // prints undefined
Is there a way to get the length data and then use it in subsequent operations?
END UPDATE
I'm a beginner with Node and Mongoose. How can I get the total number of documents in my database collection (which is called "Url")?
Some code I have tried:
1.
let len = Url.count();
2.
let len = Url.count({});
3.
let len = Url.find().count();
4.
let len = Url.find().count({});
5.
let len = function(done) {
Url.count({}, (err, data) => {
if (err) { done(err) }
else { done(null, data) }
});
};
I'm looking to get back a number, but when I console.log(len)
, I get a huge object that goes on for lines and lines: