Javascript Mongoose模型中POST变量的访问

Javascript Mongoose模型中POST变量的访问,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,使用Mongoose的平均堆栈MongoDB、ExpressJS、AngularJS和NodeJS,我正在设置一个简单的注册表单,其中包括电子邮件地址和密码字段等。我包括一个密码确认字段,以确保用户在完成注册之前知道他们键入的内容。很典型 但是,如果模式中不包含已发布的表单变量,我无法理解如何访问模型中的表单变量。我不想将密码确认字段的数据写入数据库,只需将其用于验证。我毫不怀疑这是一个微不足道的问题,但我通过搜索找到的所有内容都使用了模式中包含的字段,我已经对其进行了处理 我假设我需要编写一个

使用Mongoose的平均堆栈MongoDB、ExpressJS、AngularJS和NodeJS,我正在设置一个简单的注册表单,其中包括电子邮件地址和密码字段等。我包括一个密码确认字段,以确保用户在完成注册之前知道他们键入的内容。很典型

但是,如果模式中不包含已发布的表单变量,我无法理解如何访问模型中的表单变量。我不想将密码确认字段的数据写入数据库,只需将其用于验证。我毫不怀疑这是一个微不足道的问题,但我通过搜索找到的所有内容都使用了模式中包含的字段,我已经对其进行了处理

我假设我需要编写一个schema方法,可能还需要编写一个virtual方法,但是如何获得confirmPassword字段的值呢?如果有比你聪明的人真的能给我指出正确的方向,我将不胜感激。到目前为止我注意到的是:为了简洁起见,我省略了其他控制器方法、依赖项声明等:

注册表格

users.js控制器

user.js模型


老实说,我不会尝试对猫鼬这样做,因为它不是为了完整性而验证数据。我会先在客户端进行“Dothetwentrymatch”验证,最好不重新加载页面,然后在使用Mongoose之前在控制器中进行备份

大概是这样的:

exports.create = function(req, res) {
  // See if the match fails
  if(req.body.password !== req.body.confirmPassword) {
    return res.render('users/signup', {
      errors: {msg: "Your custom error object"},
      user: {email: req.body.email}
    });
  }
  // Otherwise carry on...
  var user = new User(req.body);
  // etc ...
});

谢谢,西蒙。我对在哪里进行验证犹豫不决。我倾向于模型,因为验证是直接构建在模型中的。在模式中定义的变量之外,我似乎无法访问发布的变量,这真的让我很烦恼。我觉得我只是错过了一些显而易见的东西,如果没有其他原因,我想揭示这一点,只是为了知道如何去做。不过,我最终可能会听从你的建议。事实证明,我从来没有能够真正做到我最初打算做的事情——验证模型中的所有内容。我相信这是可以做到的,但我会同意Simon的建议,并在控制器中进行验证。我过去经常这样做。
/**
 * Create user
 */
exports.create = function(req, res) {
  var user = new User(req.body);

  user.provider = 'local';
  user.save(function(err) {
    if (err) {
      return res.render('users/signup', {
        errors: err.errors,
        user: user
      });
    }
    req.logIn(user, function(err) {
      if (err) return next(err);
      return res.redirect('/');
    });
  });
};
/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
  Schema = mongoose.Schema,
  crypto = require('crypto'),
  _ = require('underscore');

/**
 * User Schema
 */
var UserSchema = new Schema({
  email: String,
  hashed_password: String,
  salt: String
});

/**
 * Virtuals
 */
UserSchema.virtual('password').set(function(password) {
  this._password = password;
  this.salt = this.makeSalt();
  this.hashed_password = this.encryptPassword(password);
}).get(function() {
  return this._password;
});

/**
 * Validations
 */
var validatePresenceOf = function(value) {
  return value && value.length;
};

// the below validations only apply if you are signing up traditionally (e.g. not fb, etc)
UserSchema.path('email').validate(function(email) {
  return email.length;
}, 'Email cannot be blank');

UserSchema.path('hashed_password').validate(function(hashed_password) {
  return hashed_password.length;
}, 'Password cannot be blank');


/**
 * Pre-save hook
 */
UserSchema.pre('save', function(next) {
  if (!this.isNew) return next();

  if (!validatePresenceOf(this.password))
    next(new Error('Invalid password'));
  else
    next();
});

/**
 * Methods
 */
UserSchema.methods = {
  /**
   * Authenticate - check if the passwords are the same
   *
   * @param {String} plainText
   * @return {Boolean}
   * @api public
   */
  authenticate: function(plainText) {
    return this.encryptPassword(plainText) === this.hashed_password;
  },

  /**
   * Make salt
   *
   * @return {String}
   * @api public
   */
  makeSalt: function() {
    return Math.round((new Date().valueOf() * Math.random())) + '';
  },

  /**
   * Encrypt password
   *
   * @param {String} password
   * @return {String}
   * @api public
   */
  encryptPassword: function(password) {
    if (!password) return '';
    return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
  }
};

mongoose.model('User', UserSchema);
exports.create = function(req, res) {
  // See if the match fails
  if(req.body.password !== req.body.confirmPassword) {
    return res.render('users/signup', {
      errors: {msg: "Your custom error object"},
      user: {email: req.body.email}
    });
  }
  // Otherwise carry on...
  var user = new User(req.body);
  // etc ...
});