这是我一直在线学习全栈的代码,因为这是他们使用的代码,但是我不断收到错误消息,说req.checkBody不是函数。我尝试过恢复到早期版本的express-validator,但似乎也不能这样做。显然express-validator已更新,这就是为什么req.checkBody不再是一个函数,但是我似乎无法修复我的代码的原因。
var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
var db = mongojs('passportapp', ['users']);
var bcrypt = require('bcryptjs');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
// Login Page - GET
router.get('/login', function(req, res){
res.render('login');
});
// Register Page - GET
router.get('/register', function(req, res){
res.render('register');
});
// Register - POST
router.post('/register', function(req, res){
// Get Form Values
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;
// Validation
req.checkBody('name', 'Name field is required').notEmpty();
req.checkBody('email', 'Email field is required').notEmpty();
req.checkBody('email', 'Please use a valid email address').isEmail();
req.checkBody('username', 'Username field is required').notEmpty();
req.checkBody('password', 'Password field is required').notEmpty();
req.checkBody('password2', 'Passwords do not match').equals(req.body.password);
// Check for errors
var errors = req.validationErrors();
if(errors){
console.log('Form has errors...');
res.render('register', {
errors: errors,
name: name,
email: email,
username:username,
password: password,
password2: password2
});
} else {
var newUser = {
name: name,
email: email,
username:username,
password: password
}
db.users.insert(newUser, function(err, doc){
if(err){
res.send(err);
} else {
console.log('User Added...');
//Success Message
req.flash('success', 'You are registered and can now log in');
// Redirect after register
res.location('/');
res.redirect('/');
}
});
}
});
module.exports = router;
我收到此错误:
TypeError: req.check is not a function
at E:\Desktop\website\routes\users.js:22:6
at Layer.handle [as handle_request] (E:\Desktop\website\node_modules\express\lib\router\layer.js:95:5)
at next (E:\Desktop\website\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\Desktop\website\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\Desktop\website\node_modules\express\lib\router\layer.js:95:5)
at E:\Desktop\website\node_modules\express\lib\router\index.js:281:22
at Function.process_params (E:\Desktop\website\node_modules\express\lib\router\index.js:335:12)
at next (E:\Desktop\website\node_modules\express\lib\router\index.js:275:10)
at Function.handle (E:\Desktop\website\node_modules\express\lib\router\index.js:174:3)
at router (E:\Desktop\website\node_modules\express\lib\router\index.js:47:12)
答案 0 :(得分:0)
确保已安装express-validator
:
npm i express-validator
然后像这样导入它:
var api = express.Router();
var expressValidator = require('express-validator');
api.use(expressValidator())
TypeError: req.checkBody is not a function including bodyparser and expressvalidator module
答案 1 :(得分:0)
将验证导出为验证模块,您可以使用express-validator
或更多我当前正在使用的validator
并返回错误对象。
创建文件validateRegisterInput.js会根据您的ES版本和ts更改语法
const Validator = require('validator')
module.exports = function validateRegisterInput(data) {
let errors = {}
data.email = data.email ? data.email : ''
data.password = data.password ? data.password : ''
if (!Validator.isEmail(data.email)) {
errors.email = 'Email is invalid'
}
if (Validator.isEmpty(data.email)) {
errors.email = 'Email is required'
}
if (!Validator.isLength(data.password, { min: 6, max: 30 })) {
errors.password = 'Password must have 6 chars'
}
....... // do more validation here
return {
errors,
isValid: isEmpty(errors)
}
}
在route.js文件导入上
const validateRegisterInput = require('./middleware/validateRegisterInput')
....
router.post('/register', (req, res) => {
const { errors, isValid } = validateRegisterInput(req.body)
if (!isValid) {
return res.status(400).json({
'response_code': 400,
'message': 'error',
'errors': errors
})
} else {
// create User
}
})