Mic*_*abe 8 mongoose mongodb node.js passport.js
使用Mongoose.js,我的authenticate方法填充字段"companyRoles._company",但当我尝试访问req.user对象中相同的填充字段时,填充的数据将恢复为公司引用ID.
//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);
});
Run Code Online (Sandbox Code Playgroud)
在req.logIn之后,记录req.user显示 - companyRoles {_company:[Object]}
但是当我登录后重定向到/ companies /:id route时,它显示的是id而不是填充的[object] - companyRoles {_company:4fbe8b2513e90be8280001a5}
关于为什么该领域不会继续填充的任何想法?谢谢.
Mic*_*abe 12
问题是我没有填写passport.deserializeUser函数中的字段,这里是更新的函数:
//deserialize
passport.deserializeUser(function(id, done) {
User.findById(id)
.populate('companyRoles._company', ['name', '_id'])
.run(function (err, user) {
done(err, user);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4861 次 |
| 最近记录: |