我有3个字段的表单:textfield1,textdield2和input3。我渲染它。
router.get('/myform', function(req, res) {
if(!req.session.passport){
res.location('/');
res.redirect('/');
}
res.render('user/myform', {
title: 'Быстрая проверка',
user: req.session.passport.user
});
});
当我尝试验证它时,我可以发现一些错误。这就是为什么我使用快速验证器。
router.post('/myform', function(req, res) {
var textfield1= req.body.textfield1;
var textdield2 = req.body.textdield2;
var input3 = req.body.input3;
req.checkBody('textdield1','Cant be empty!').notEmpty();
req.checkBody('textdield2 ','Cant be empty!').notEmpty();
req.checkBody('input3','Cant be empty!').notEmpty();
var errors = req.validationErrors();
if(errors){
var form = new Object();
form.textfield1 = textfield1;
form.textdield2 = textdield2;
form.input3 = req.body.input3;
res.render('user/myform',{
errors: errors,
title:"Errors here!",
form: form
});
} else { ...
一切正常,但我的中间件并不想在所有工作。例如:
app.get('*', function(req, res, next) {
if(req.isAuthenticated()){
res.locals.weauth = true;
User.getUserById(req.session.passport.user, function(err, user) {
if(err) console.log(err);
if(!user.name) res.locals.username = user.username;
else res.locals.username = user.name;
res.locals.balance = user.balance;
next();
});
} else {
res.locals.weauth = null;
next();
}
});
我做错了什么以及如何解决这个问题。谢谢!