req.checkBody不是函数,我需要解决此问题

时间:2018-09-07 09:20:39

标签: node.js

TypeError: req.checkBody is not a function
    at C:\techscape\nodeauth\routes\users.js:41:7
    at Layer.handle [as handle_request] (C:\techscape\nodeauth\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\techscape\nodeauth\node_modules\express\lib\router\route.js:137:13)
    at Array.<anonymous> (C:\techscape\nodeauth\node_modules\multer\lib\make-middleware.js:53:37)
    at listener (C:\techscape\nodeauth\node_modules\on-finished\index.js:169:15)
    at onFinish (C:\techscape\nodeauth\node_modules\on-finished\index.js:100:5)
    at callback (C:\techscape\nodeauth\node_modules\ee-first\index.js:55:10)
    at IncomingMessage.onevent (C:\techscape\nodeauth\node_modules\ee-first\index.js:93:5)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

代码:

var express = require('express');
var router = express.Router();

var multer=require('multer');
var upload=multer({dest:'./uploads'});

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

router.get('/login', function(req, res, next) {
  res.render('login');
});

router.get('/signup', function(req, res, next) {
  res.render('signup');
});

router.post('/signup',upload.single('profileimg'), function(req, res, next) {
  // console.log(req.body.username);
  // console.log(req.body.email);
  // console.log(req.body.password);
  // console.log(req.body.password2);
  //  console.log(req.file);
  var username=req.body.username;
  var email=req.body.email;
  var pass=req.body.password;
  var pass2=req.body.password2;
  //var file=req.file;

  if(req.file){
    console.log("uploading file ");
    var image=req.file.filename;
  }else{
    console.log("NO files uploaded ");
  }


  //Validators 
  req.checkBody('username','UserName is required').isEmpty();
  // req.checkBody('email','email is Required').isEmail();
  // req.checkBody('password','Passwrd is required').isEmpty();
  // req.checkBody('password2','email is Required').equal(req.password);





});

router.post('/login', function(req, res, next) {

  var username=req.body.username;
  var password=req.body.pass;
  // console.log(req.body.username);
  // console.log(req.body.pass);
});

    enter code here

module.exports = router;

1 个答案:

答案 0 :(得分:2)

req.checkBody()不是标准的ExpressJS函数。
这是express-validator模块提供的功能。

您需要先安装它:

$ npm i express-validator

,然后引用它:

var express = require('express');
var expressValidator = require('express-validator');
var router = express.Router();

router.use(expressValidator());

// ...