所以我正在使用app.use(expressValidator());作为中间件,当我在服务器上吃午饭时,收到一条错误消息,提示“ TypeError:expressValidator不是函数” 我记得过去曾经使用相同的代码没有任何问题,我认为V6可能有所更改。有什么主意吗?
谢谢
答案 0 :(得分:1)
“ expressValidator”是指https://www.npmjs.com/package/express-validator吗?
如果是这样,您可以像这样使用它
const { check, validationResult } = require('express-validator');
app.post('/user', [
// username must be an email
check('username').isEmail(),
// password must be at least 5 chars long
check('password').isLength({ min: 5 })
], (req, res) => {
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
User.create({
username: req.body.username,
password: req.body.password
}).then(user => res.json(user));
});
的链接