TypeError:req.checkBody不是函数。快速验证器不起作用,验证失败了吗?

时间:2017-10-30 05:02:39

标签: javascript node.js express express-validator

我正在定义一个处理提交请求的邮政路线。我在user.js route中编写了以下代码:

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不是函数。请告诉我代码中缺少的内容或所需的任何修改。

已经通过下面提到的链接,但它对我不起作用: TypeError: req.checkBody is not a function

1 个答案:

答案 0 :(得分:0)

我已经制作了简单的应用程序,尝试根据此重写代码,它工作正常

app.js文件

Could not find goal '' in plugin ...

你可以跳过身体解析器和角色。 users.js文件看起来像

var express = require('express');
var expressValidator = require('express-validator');
var app = express();
var router = express.Router();
var bodyParser = require("body-parser");
var cors = require('cors');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());

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
        };
    }
}));
// I pass app to the users.js file, because express validator I've assigned 
// to this express app instance
require('./users')(app);

app.listen(3300);

我使用app.post('/ register'),因为快递路由器必须实例化,如果你创建路由器到新的快递对象,你将没有连接到快速验证器,因为那个快速验证器被分配给其他快递您在app.js文件中创建的对象。