使用Mongoose模式验证用户名

时间:2016-02-04 08:52:59

标签: javascript node.js mongodb mongoose mongoose-schema

我尝试在将用户名保存到mongodb之前验证用户名。但相反,保存或验证消息我在终端中看到以下消息:

"如果(user.path(用户名)){ TypeError:user.path不是函数"

这意味着什么? 我是新手。

这是我的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({});
});

这是我的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);

1 个答案:

答案 0 :(得分:0)

在保存到vaildate之前你不需要调用任何东西,mongoose会为你做这件事。在完成之前,请不要致电callback

在执行console.log(err.message)之前,请检查是否发生错误,因为如果现在发生错误,errnull

所以这应该有效:

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