2016-02-04 117 views
0

我試圖驗證用戶名,然後將它保存到mongodb。而是保存或驗證消息我看到在終端以下消息:用Mongoose模式驗證用戶名

「如果(user.path(用戶名)){ 類型錯誤: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); 

回答

0

你不需要保存到vaildate之前調用任何東西,貓鼬會爲你做它。在完成之前也不要撥打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); 
    }); 
} 
+0

它的作品,謝謝! –