javascript - Accessing POST variables in Mongoose model -


using mean stack (mongodb, expressjs, angularjs, , nodejs) mongoose, i'm setting simple registration form include email address , password fields, among others. i'm including password confirmation field ensure users know they've typed before completing registration. pretty typical.

however, can't figure out how access posted form variables in model if they're not included in schema. don't want write password confirmation field's data db, use validation. have no doubt it's trivial issue, i've found searching has used fields included in schema, i've got handle on.

i'm assuming need write schema method, , maybe virtual, how value of confirmpassword field? if brighter yours point me in right direction, i'd obliged. here's have far (note: omitted other controller methods, dependency declarations, etc. brevity):

signup.jade (form)

  form.signup(action="/users", method="post")      .control-group       label.control-label(for='email') email       .controls         input#email(type='text', name="email", placeholder='email', value=user.email)      .control-group       label.control-label(for='password') password       .controls         input#password(type='password', name="password", placeholder='password')      .control-group       label.control-label(for='confirmpassword') confirm password       .controls         input#password(type='password', name="confirmpassword", placeholder='confirm password')      //- birthdate     include ../shared/birthdate      .form-actions       button.btn.btn-primary(type='submit') sign               | or        a.show-login(href="/login") log in 

users.js (controller)

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

user.js (model)

/**  * 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; };  // below validations apply if signing traditionally (e.g. not fb, etc) userschema.path('email').validate(function(email) {   return email.length; }, 'email cannot blank');  userschema.path('hashed_password').validate(function(hashed_password) {   return hashed_password.length; }, 'password cannot 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 passwords 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); 

to honest not try mongoose, it's not validating data sake of integrity. 'do 2 entries match' validation firstly on client side (preferably without reloading page), , secondly - backup - in controller before doing mongoose.

something this:

exports.create = function(req, res) {   // see if 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 ... }); 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -