Using Mongoose.js, my authentication method populates the companyRoles._company field, but the populated data returns to the company link identifier when I try to access the same populated field in my req.user object.
//Authentication UserSchema.static('authenticate', function(email, password, callback) { this.findOne({ email: email }) .populate('companyRoles._company', ['name', '_id']) .run(function(err, user) { if (err) { return callback(err); } if (!user) { return callback(null, false); } user.verifyPassword(password, function(err, passwordCorrect) { if (err) { return callback(err); } if (!passwordCorrect) { return callback(null, false); } return callback(null, user); }); }); }); //login post app.post('/passportlogin', function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (err) { return next(err) } if (!user) { return res.redirect('/passportlogin') } req.logIn(user, function(err) { if (err) { return next(err); } console.log('req User'); console.log(req.user); return res.redirect('/companies/' + user.companyRoles[0]._company._id); }); })(req, res, next); }); app.get('/companies/:cid', function(req, res){ console.log('req.user in companies:cid'); console.log(req.user); });
After req.logIn, the req.user log shows - companyRoles {_company: [Object]}
But when I redirect the route / companies /: id after login, it shows the identifier, not the filled [object] - companyRoles {_company: 4fbe8b2513e90be8280001a5}
Any ideas as to why the field does not remain populated? Thanks.
Michael mccabe
source share