TypeError: req.checkBody is not a function including bodyparser and expressvalidator module

前端 未结 1 1246
北荒
北荒 2021-01-29 04:55

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

Here is my code

var express = require(\'exp         


        
相关标签:
1条回答
  • 2021-01-29 05:29
    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 affffded
    
    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!" });
    
            });
    
        });
    
    0 讨论(0)
提交回复
热议问题