I am writing a NodeJS server with ExpressJS, PassportJS, MongoDB and MongooseJS. I just managed to get PassportJS to use user data obtained via Mongoose to authenticate.
But to make it work, I had to use a “findById” function like below.
var UserModel = db.model('User',UserSchema);
UserModel.findById(id, function (err, user) { < SOME CODE > } );
UserModel is a Mongoose model. I declare the schema, UserSchema earlier. So I suppose UserModel.findById() is a method of the Mongoose model?
Question
What does findById do and is there documentation on it? I googled around a bit but didn’t find anything.
findByIdis a convenience method on the model that’s provided by Mongoose to find a document by its _id. The documentation for it can be found here.Example:
Functionally, it’s the same as calling:
Note that Mongoose will cast the provided
idvalue to the type of_idas defined in the schema (defaulting to ObjectId).