This is the expected behaviour of cursor.each. The null value indicates there are no more results (ie the cursor is exhausted/empty and closed). If you don't want the extra null call when the cursor is finished, you can use cursor.forEach:
db.collection('Carreer').find().forEach(function(doc) {
// handle
}, function(err) {
// done or error
});
Also note:
cursor.each is deprecated.
Why the null?
The cursor.each method just has one callback and the only way to know the cursor has finished is the last null. An example implementation could be:
function fetchAllCareers(callback) {
const results = [];
const cursor = db.collection('Carreer').find();
cursor.each(function(error, result) {
if (error) {
callback(error);
return;
}
if (result === null) { // This was the last iteration, we finished
callback(null, results);
return;
}
results.push(result);
});
}
The cursor.forEach method on the other hand has an an iterator callback and a second callback to let you know that the iterations have finished. An example implementation could be:
function fetchAllCareers(callback) {
const results = [];
const cursor = db.collection('Carreer').find();
cursor.forEach(function(result) {
results.push(result);
}, function(error) { // all iterations finished
callback(error, results);
});
}