I saw a couple of post with similar to mine but I still getting the same error
here is my user schema
I had the same problem with mongoose
version > 4.7.2
Problem is about bson package.
I solved it with installing an older version of mongoose.
npm install mongoose@4.7.2
or you can change package.json to use exact version 4.7.2 "mongoose": "4.7.2"
You can update to newer versions after the problem is solved. You can track it on here.
The error exists in your serializeUser
function in passport
.
You need to use user._id
instead of user.id
.
since there is no field
as id
in your UserSchema
, user.id
will beundefined
, and while deserializing
the user, undefined
is not typeOf
ObjectId
, thus it is throwing above error.
Try this:
passport.serializeUser(function(user, done) {
done(null, user._id);
});
Update:
Do this in your deserializeUser
:
cast the upcoming id
to ObjectId
, just to be sure, and then use that ID
to query
the User
.
var userId = mongoose.Schema.Types.ObjectId(id);
User.findById(userId , function(err, user) {
done(err, user);
});
Dont forget to include mongoose
in the same file.
var mongoose=require('mongoose');
Hopefully this will help you.