0

我正在尝试在将其保存到 mongodb 之前验证用户名。但是,我在终端中看到了以下消息,而不是保存或验证消息:

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

这是什么意思?我是新手。

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

1 回答 1

0

在保存到 vaildate 之前你不需要调用任何东西,猫鼬会为你做这件事。也不要打电话callback,直到你完成。

还要在做之前检查是否发生错误,console.log(err.message)因为err如果null现在发生错误。

所以这应该工作:

} else {
    var user = new User({username: username, password: password});
    user.save(function(err) {
        if (err) {
            console.log(err.message);
            return next(err);
        }
        callback(user);
    });
}
于 2016-02-04T09:53:38.697 回答