I just encountered a strange behavior. I've a MongoDB collection allCustomers
and trying to fill it up, as long as the Customers.countDocuments()
returns a value lower than 3. Model and Schema are exported and properly accessible, since it's adding the entry by using if
, instead of while
. Unfortunately that's not my requirement and I it's not reasonable to me.
let Customer = require('./models/customer');
router.get('/', function (req, res) {
Customer.countDocuments({}, function (err, count) {
while (count < 3) {
var emptySlot = new Customer({ name: 'Unused', gender: 'empty' });
emptySlot.save(function (err, slot) {
if (err) return console.error(err);
console.log(slot.name);
});
}
});
Customer.find({}, function (err, customers) {
..
});
});