Sachin
Sachin

Reputation: 603

req.user gets changed to Array object

I'm facing a weird issue after I ran npm install.

The instance of req.user when accessing in `requireManager() (or any controller) is of type Array rather then the object returned from passport.deserializeUser.

passport.deserializeUser(function(id, done) {
    compound.models.User.findById(id, function(err, user) {
         done(err, user);
    });
});

console.log(req.user.constructor) in controller prints

function Array() { [native code] }   

and

console.log(user.constructor)  in `passport.deserializeUser` prints:


 function model(doc, fields, skipId) {
        if (!(this instanceof model))
          return new model(doc, fields, skipId);
        Model.call(this, doc, fields, skipId);
    }

Kindly help me to resolve this issue.

Regards, Sachin

Upvotes: 0

Views: 589

Answers (1)

Nikoloz Shvelidze
Nikoloz Shvelidze

Reputation: 1614

In case someone ends up here from a Google search (like me).

In passport.deserializeUser, using findById on a model returns an array, either use findOne or pass user[0] to done.

passport.deserializeUser(function (id, done) {
    myModel.findOne(id, function (err, user) {
        done(err, user);
    });
});

Upvotes: 2

Related Questions