Mongodb 无法哈希平均堆栈中的密码

Mongodb 无法哈希平均堆栈中的密码,mongodb,express,mongoose,passport.js,mean-stack,Mongodb,Express,Mongoose,Passport.js,Mean Stack,这是routes文件中的代码 router.put('/reset/:token', function(req, res, next) { console.log('reseting the password'); User.findOne({resetPasswordToken:req.params.token}, function(err, user) { if(err) { return next(err); } if (!user) {

这是routes文件中的代码

router.put('/reset/:token', function(req, res, next) {
  console.log('reseting the password');
  User.findOne({resetPasswordToken:req.params.token}, function(err, user) {
    if(err) {
      return next(err);
    }
    if (!user) {
      return res.status(422).json({errors: [{msg: 'invalid reset token'}]});
    }

    user.resetPasswordToken ='';
    user.resetPasswordExpires = '';
    user.password = req.body.password;
    User.addUser(user, (err, user) => {
      if(err){
        res.json({success: false, msg:'password has not changed'});
      } else {
        res.json({success: true, msg:'password has changed'});
      }
    });
  });
});
这部分代码来自我的模式文件

const UserSchema = mongoose.Schema({
  password: {
    type: String,
    required: true
  },

  resetPasswordToken: {
    type: String
  },
  resetPasswordExpires: {
    type: Date
  }

});

  const User = module.exports = mongoose.model('User', UserSchema);
module.exports.addUser = function(newUser, callback){
    bcrypt.genSalt(10, (err, salt) => {
      bcrypt.hash(newUser.password, salt, (err, hash) => {
        if(err) throw err;
        newUser.password = hash;
        newUser.save(callback);
      });
    });
  }

当我尝试休息的密码,它是存储,因为我已经给出了输入。它不是对密码进行哈希运算。例如,我将密码设置为“zp12345”,它在数据库中存储为“密码”:“zp12345”。

要解决需要修复addUser方法的问题,请执行以下操作:

var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

module.exports.addUser = function(newUser, callback){
    bcrypt.hash(newUser.password, bcrypt.genSaltSync(10), null, (err, hash) => {
       if (err) {
         return next(err);
       }
       newUser.password = hash;
       newUser.save(callback);
    })
};
这里还有另一个例子:


这是库文档:

散列可以创建新用户吗?如果没有,您可能希望使用
bcrypt nodejs
模块,而不是下面回答中描述的
bcrypt
模块。我还遇到了一些关于
bcrypt
模块的问题。是的,它可以工作。我已经解决了。太好了!:)您可以编辑您的原始帖子,以提供错误答案,以防其他人在使用
passport时遇到类似问题。