I have form with 3 fields: textfield1, textdield2 and input3. I render it.
router.get('/myform', function(req, res) {
if(!req.session.passport){
res.location('/');
res.redirect('/');
}
res.render('user/myform', {
title: 'Быстрая проверка',
user: req.session.passport.user
});
});
When i try to validate it i can catch some errors. That why i use express validator.
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 { ...
All works fine, but my middleware didnt want to works atall. For example this:
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();
}
});
What i do wrong and how to fix this. Thanks!
via Nikita M
No comments:
Post a Comment