我一直在争论这个问题已经有一段时间了,我有这个Nodejs Express应用程序,当表单字段无效时,我的注册表单会返回一堆验证错误,这是一件好事但是,当表单填写正确时它仍然会返回这些错误,并且不会提交任何数据。
这是寄存器的代码route.post
router.post('/register', function(req, res, next){
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;
// Check for image field
if(req.files && req.files.profileimage){
console.log('Uploading File...');
// File Info
var profileImageOriginalName = req.files.profileimage.originalname;
var profileImageName = req.files.profileimage.name;
var profileImageMime = req.files.profileimage.mimetype;
var profileImagePath = req.file.profileimage.path;
var profileImageExt = req.files.profileimage.extension;
var profileImageSize = req.files.profileimage.size;
}else {
// Set a Default Image
var profileImageName = 'noimage.png';
}
// Form validation
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('email', 'Invalid Email ID').isEmail();
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('password', 'Password field is required').notEmpty();
req.checkBody('password2', 'Password do not match').equals(req.body.password);
// Check for Validation errors
var errors = req.validationErrors();
if(errors){
res.render('register', {
errors: errors,
name: name,
email: email,
username: username,
password: password,
password2: password2
});
}else {
var newUser = new User({
name: name,
email: email,
username: username,
password: password,
profileimage: profileImageName
});
// Create User
User.createUser(newUser, function(err, user){
if(err) throw err;
console.log(user);
});
// Success Message
req.flash('success', 'You are now registered and may log in');
res.location('/');
res.redirect('/');
}
});
这是我的register.jade代码
extends layout
block content
h1 Register
p Please register using the form below
ul.errors
if errors
each error, i in errors
li.alert.alert-danger #{error.msg}
form(method='post', action='/users/register', enctype='multipart/form-data')
.form-group
label Name
input.form-control(name='name', type='text', placeholder='Enter Name')
.form-group
label Email
input.form-control(name='email', type='text', placeholder='Enter Email')
.form-group
label Username
input.form-control(name='username', type='text', placeholder='Enter Username')
.form-group
label Password
input.form-control(name='password', type='password', placeholder='Enter Password')
.form-group
label Confirm Password
input.form-control(name='password2', type='password', placeholder='Confirm Password')
.form-group
label Profile Image
input.form-control(name='profileimage', type='file')
input.btn.btn-default(name='submit', type='submit', value='Register')
请任何帮助将非常感谢...谢谢
答案 0 :(得分:0)
正文解析器不能将多部分表单属性设置为表单数据,而是设置为urlencoded,这是
<form action="/register" method="post" multipart="urlencoded"></form>
你应该好!谢谢大家的时间。保持幸福
答案 1 :(得分:0)
1) Use body parser and express-validator before router you used
2) if you use multipart/form-data then you use validator middleware after multer file upload strategy
这是我的工作
var validator = function(req,res,next){
req.checkBody('name').notEmpty().withMessage('Name field is required');
}
router.post('/upload',multer.single('avatar'),validator,function(req,res,next){
//req.body
//req.file
both object available here
})