1
我正在嘗試使用Express和Mongoose爲用戶輸入做一些驗證。以前,我在用戶模型的'密碼'字段直接設置了'最小長度'驗證程序。它會檢查輸入的密碼字符串是否足夠長,如果不是,則會將其傳遞給嘗試保存用戶時創建的ValidationError對象。這將被髮回給用戶,並顯示在適當的表單域中。貓鼬錯誤處理
但現在,我已經實現了哈希,鹽和加密,這並不完全正常工作。散列版本的密碼比原始密碼長很多。所以,我想我可以做這樣的事情:
exports.createNewUser = function(req, res, next){
var user = new User({
email : req.body.email
, name : req.body.name
})
, minLength = Validator.minLength(req.body.password);
if (!minLength) return next(new Error('Password aint right length'));
user.setPassword(req.body.password);
user.save(function(err){
if (!err) return res.redirect('/');
else {
console.log('error saving usr', err);
err['body'] = req.body;
return res.render('user/register', err);
}
});
};
的問題是,
return next(new Error('err message'));
僅僅是在控制檯上顯示。我需要一些方法將它附加到貓鼬驗證錯誤對象。有任何想法嗎?