Javascript 保存Mongo文档时出现异常

Javascript 保存Mongo文档时出现异常,javascript,express,mongoose,passport.js,mean-stack,Javascript,Express,Mongoose,Passport.js,Mean Stack,在平均堆栈应用程序中,我在用户模式中创建一个新用户,然后在文档模式中创建一个新文档 var UserSchema = new Schema({ username: String, password: String, docs: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Doc'

在平均堆栈应用程序中,我在用户模式中创建一个新用户,然后在文档模式中创建一个新文档

    var UserSchema = new Schema({
            username: String,
            password: String,
            docs: [{
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Doc'
            }],
        )
    }

    var DocSchema = new Schema({…)
    }

    UserSchema.pre('save', function(next) {
        if (this.password) {
            this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
            this.password = this.hashPassword(this.password);
        }
        next();
    });
下面的代码部分是passport注册,最后我对newUser.save()有一个问题;在推送方法之后,如果我不保存用户,则文档ID不会显示在用户文档中。但保存用户似乎也会更改散列密码。如果我注释newUser.save();登录工作正常,否则我会得到一个错误的密码

    passport.use('local-signup', new LocalStrategy({
              usernameField: 'username',
              passwordField: 'password',
              passReqToCallback: true
      },
      function(req, username, password, done) {

          process.nextTick(function() {

                  User.findOne({
                          username: username
                      }, function(err, user) {
                          // if there are any errors, return the error
                          if (err)
                              return done(err);
                          if (user) {
                              return done(null, false, req.flash('signupMessage', 'Username is already taken.'));
                          } else {
                              var newUser = new User();
                              newUser.username = username;
                              newUser.password = password;
                              newUser.email = req.body.email;
                              newUser.save(function(err) {
                                  if (err)
                                      throw err;
                                  var doc = new Doc({
                                      user: newUser.username,
                                      docTitle: newUser.username
                                  });
                                  doc.save(function(err) { // create doc
                                      if (err) {
                                          return next(err);
                                      } else {
                                          newUser.docs.push(doc); // push doc'id in docs field in user
                                          newUser.save(); // save user after doc'id has been push
                                      };
                                      return done(null, newUser);
                                  });
                              });

任何帮助都将不胜感激

您在mongoose预保存中间件中的逻辑是“如果要保存的文档上有密码,请生成salt并对密码进行哈希”。因此,如果已经对文档进行了加密和散列处理的文档上存在密码,那么当中间件运行时,它将再次加密和散列这个预先存在的密码。这就是为什么你不能再次登录;每次保存文档时,密码都会更改

UserSchema.pre('save',  function(next) {
    if (this.password && this.isNew) {
        this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
        this.password = this.hashPassword(this.password);
    }
    next();
});
我猜您希望mongoose预保存中间件仅在第一次保存文档时运行。每次保存文档时,预保存中间件都会运行。预保存中间件中的文档上有一个
this.isNew
属性可供使用。这将确保仅在首次保存文档时生成密码

UserSchema.pre('save',  function(next) {
    if (this.password && this.isNew) {
        this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
        this.password = this.hashPassword(this.password);
    }
    next();
});

非常感谢@sabrehagen的清晰解释-它就像一个魅力!