小编Mic*_*abe的帖子

Passport.js和Mongoose.js在登录时填充用户 - 在req.user上丢失填充的字段

使用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) { …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js passport.js

8
推荐指数
1
解决办法
4861
查看次数

标签 统计

mongodb ×1

mongoose ×1

node.js ×1

passport.js ×1