简体   繁体   中英

Validating username with Mongoose schema

I'm trying to validate username before save it to mongodb. But instead saving or validation message i see the following message in terminal:

" if(user.path(username)){ TypeError: user.path is not a function"

What does it means? I am newbie.

Here is my user.js

var User = require('models/user').User;
var HttpError = require('error').HttpError;
var async = require('async');


exports.get = function(req, res) {
res.render('login', { title: 'Login'});
};

exports.post = function(req, res, next) {
  var username = req.body.username;
  var password = req.body.password;

async.waterfall([
    function(callback) {
        User.findOne({username: username}, callback);
    },
    function(user, callback) {
        if (user) {
            if (user.checkPassword(password)) {
                callback(null, user);
            } else {
                next(new HttpError(403, "wrong password"));
            }
        } else {
            var user = new User({username: username, password: password});
            if(user.path(username)){
                callback(null, user);
                user.save(function(err) {
                    console.log(err.message)
                    if (err)
                        return next(err);
                    callback(user);
                });
            }else{ next(new HttpError(403, "Incorrect username"));

            };
        }
    }
], function(err, user){
    if (err) return next(err);
    req.session.user = user._id;
    res.send({});
});

and here is my login.js

var crypto = require('crypto');

var mongoose = require('lib/mongoose'),
Schema = mongoose.Schema;

var schema = new Schema({
username: {
    type: String,
    unique: true,
    required: true
},

hashedPassword: {
    type: String,
    required: true
},
salt: {
    type: String,
    required: true
},
created: {
    type: Date,
    default: Date.now
}
});


schema.path('username').validate(function (value, respond) {
  return /[0-9]{6,15}[a-zA-Z]/.test(value, function(){
    respond(false, 'this message gets to the validation error');
});
}, '{VALUE} is not a valid login - [0-9]{6,15}[a-zA-Z]')


schema.methods.encryptPassword = function(password) {
  return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
};

schema.virtual('password')
  .set(function(password) {
      this._plainPassword = password;
      this.salt = Math.random() + '';
      this.hashedPassword = this.encryptPassword(password);
})
.get(function() { return this._plainPassword; });


schema.methods.checkPassword = function(password) {
  return this.encryptPassword(password) === this.hashedPassword;
};

schema.path('username').validate(function (value) {
  return /[0-9]{6,15}[a-zA-Z]/.test(value);
}, 'Invalid color');

exports.User = mongoose.model('User', schema);

You don't need to call anything before saving to vaildate, mongoose will do it for you. Also don't call callback until you are done.

Also check if error occur before doing console.log(err.message) because err is null if now error happens.

So this should work:

} else {
    var user = new User({username: username, password: password});
    user.save(function(err) {
        if (err) {
            console.log(err.message);
            return next(err);
        }
        callback(user);
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM