gpt4 book ai didi

node.js - req.checkBody 不是一个函数,我需要解决这个问题

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:39 25 4
gpt4 key购买 nike

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;

最佳答案

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

您需要先安装它:

$ npm i 快速验证器

然后引用它:

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

router.use(expressValidator());

// ...

关于node.js - req.checkBody 不是一个函数,我需要解决这个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52219375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com