error ReferenceError: ObjectID is not defined error ReferenceError: ObjectID is not defined mongodb mongodb

error ReferenceError: ObjectID is not defined


Not directly a solution but a small typo wasted 10 minutes of my time:

var ObjectId = require('mongodb').ObjectID;

I declared a variable ObjectId with small d but when using the variable, I tried ObjectID with capital D.


ObjectID is not a global variable, it is defined by mongoose (see the documentation):

var fileId = mongoose.Types.ObjectId();


Try This. It'll work in your code properly.

var ObjectId = require('mongodb').ObjectID;