保存 Mongo 个异常文档

Save a Mongo document with exception

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

    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();
    });

以下代码部分是护照注册,最后我遇到了 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 pre save 中间件中的逻辑是 'if there is a password on the document being saved, generate a salt and hash the password'。因此,如果文档中存在已经加盐和散列的密码,当中间件 运行s 时它将再次加盐和散列这个预先存在的密码。这就是为什么您不能第二次登录的原因;每次保存文档时,您的密码都会更改。

我猜您希望 mongoose 预保存中间件仅在您第一次保存文档时 运行。每次保存文档时预保存中间件运行s。在预保存中间件的文档中有一个 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();
});