我正在定义一个处理提交请求的发布路线。我在 user.js 路由中编写了以下代码:
var express = require('express');
var router = express.Router();
var multer = require('multer');
var upload = multer({dest:'./uploads'});
router.post('/register', upload.single('profileimage'), 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;
if(req.file){
var profileImage = req.file.filename;
}
else{
var profileImage = 'noimage.jpg';
}
//form validation
req.checkBody('name', 'Name feild is reqiured').notEmpty();
req.checkBody('email', 'Email feild is reqiured').notEmpty();
req.checkBody('email', 'Email feild is reqiured').isEmail();
req.checkBody('username', 'Name feild is reqiured').notEmpty();
req.checkBody('password', 'PasswordUser feild is reqiured').notEmpty();
req.checkBody('password2', 'Password donot match').equals(req.body.password);
//check Errors
var errors = req.validationErrors();
if(errors){
res.render('register', {
errors: errors
});
console.log(errors);
}
else{
Console.log('No Errors');
}
});
我的 app.js 已经在其中定义了 express-validator,如下所示:
var expressValidator = require('express-validator');
var app = express();
app.use(expressValidator({
errorFormatter: function(param, msg, value){
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift()
}
return{
param: formParam,
msg: msg,
value: value
};
}
}));
每次单击提交按钮测试验证时,浏览器上都会显示错误TypeError: req.checkBody is not a function 。请让我知道代码中缺少什么,或需要任何修改。
已经通过下面提到的链接,但它对我不起作用。: TypeError:req.checkBody 不是函数