类型错误:req.checkBody 不是包含 bodyparser 和 expressvalidator 模块的函数 [英] TypeError: req.checkBody is not a function including bodyparser and expressvalidator module

查看:23
本文介绍了类型错误:req.checkBody 不是包含 bodyparser 和 expressvalidator 模块的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误 req.checkBody is not a function 认为我已经包含了 express-validator 和 body-parser..

I'm getting error req.checkBody is not a function thought I've included express-validator and body-parser..

这是我的代码

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var expressValidator = require('express-validator');

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

app.get('/',function(req,res){
    res.sendFile(__dirname +'/index.html')
});

app.post('/',function(req,res){
        req.checkBody("name", "Invalid company").notEmpty();

        var errors = req.validationErrors();
        if (errors) {
            res.status(400).send({ "message": "Missing parameter" });
            // res.send('There have been validation errors: ' + util.inspect(errors), 400);
            return;
        }
        var company = new companySchema(req.body);
        company.save(function(err) {
            if (err) {
                if (err.code === 11000) {
                    return res.status(409).send({ "message": "Company already exist!" });
                }
                return res.status(400).send({ "message": "Server Error!", "err": err });
            }

            return res.status(200).send({ "message": "New company has added!" });

        });

    });

app.listen('3000');
console.log('listening on 3000!')

编辑部分包含 index.html 以供参考

Edited part is included index.html for reference

index.html

<html>
<body>
 <form method = "post" action="http://localhost:3000">
    <label for="fname">Company name</label>
    <input type="text" id="fname" name="name">        
    <input type="submit" value="Submit">
  </form>
</body>
</html>

我得到的错误是

类型错误:req.checkBody 不是函数

TypeError: req.checkBody is not a function

推荐答案

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var expressValidator = require('express-validator');

app.use(bodyParser.urlencoded({ extended: true })); 
app.use(expressValidator());  //this line to be addded

app.get('/',function(req,res){
    res.sendFile(__dirname +'/index.html')
});

app.post('/',function(req,res){
        req.checkBody("name", "Invalid company").notEmpty();

        var errors = req.validationErrors();
        if (errors) {
            res.status(400).send({ "message": "Missing parameter" });
            // res.send('There have been validation errors: ' + util.inspect(errors), 400);
            return;
        }
        var company = new companySchema(req.body);
        company.save(function(err) {
            if (err) {
                if (err.code === 11000) {
                    return res.status(409).send({ "message": "Company already exist!" });
                }
                return res.status(400).send({ "message": "Server Error!", "err": err });
            }

            return res.status(200).send({ "message": "New company has added!" });

        });

    });

这篇关于类型错误:req.checkBody 不是包含 bodyparser 和 expressvalidator 模块的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆