我正在尝试安装和使用express-validator软件包。我已经安装了软件包版本(6.0.0),然后在server.js文件中,代码是:
const bodyParser = require('body-parser')
const expressValidator = require('express-validator')
const express = require('express')
const nunjucks = require('nunjucks')
const sessionInMemory = require('express-session')
const cookieParser = require('cookie-parser')
然后在下面几行中输入:
const app = express()
const documentationApp = express()
app.use(expressValidator())
服务器(使用nodemon)重新加载更改时,应用程序崩溃并显示:
TypeError:expressValidator不是函数
server.js文件中还有其他代码,但是我已经剔除了其中大部分与我无关的代码。
expressValidator的控制台日志:
{ oneOf: [Function: oneOf],
buildSanitizeFunction: [Function: buildSanitizeFunction],
sanitize: [Function],
sanitizeBody: [Function],
sanitizeCookie: [Function],
sanitizeParam: [Function],
sanitizeQuery: [Function],
buildCheckFunction: [Function: buildCheckFunction],
check: [Function],
body: [Function],
cookie: [Function],
header: [Function],
param: [Function],
query: [Function],
checkSchema: [Function: checkSchema],
matchedData: [Function: matchedData],
validationResult: { [Function] withDefaults: [Function: withDefaults] },
Result: [Function: Result] }
routes.js文件的代码:
router.get('/email-adress', function (req, res) {
res.render('email-adress', { success: req.session.success, errors: req.session.errors })
req.session.errors = null
})
router.post('/finished', function (req, res) {
let email = req.body.email
req.checkBody('email', 'Email required').isEmail()
var errors = req.validationErrors()
if (errors) {
req.session.errors = errors
req.session.success = false
res.redirect('/email-adress')
} else {
req.session.success = true
res.redirect('/finished')
}
})
答案 0 :(得分:1)
const { check, validationResult } = require('express-validator');
router.post('/finished', function (req, res) {
let email = req.body.email
check('email', 'Email required').isEmail()
var errors = validationResult(req)
if (errors) {
req.session.errors = errors
req.session.success = false
res.redirect('/email-adress')
} else {
req.session.success = true
res.redirect('/finished')
}
})
执行此操作。然后删除
app.use(expressValidator())
行。
答案 1 :(得分:1)
//just pass the checking as middleware not in the callback
//see here I've just passed an array for checking as middleware
// as the middleware is an array therefore you can add multiple checks in the array
router.post("/", [check('email', "your custom error message").isEmail()], (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.render('errorPage', { errors: errors.array() });
//if api caller return res.status(422).json({ errors: errors.array() });
}
else{
//here everything is ok to proceed
res.render('successPage', { data });
//to api caller res.json({msg : "ok"})
}
})
答案 2 :(得分:1)
Express Validator已更新,您不能以这种方式使用它 This is the new way使用快速验证器
或者,您可以通过运行以下命令来保留以前的版本
npm uninstall express-validator
npm install express-validator@5.3.0
答案 3 :(得分:1)
just update the express validator , will do the tri
npm install express-validator@5.3.1 --save-exact
答案 4 :(得分:0)
是的!即使我遇到了同样的问题,也花了6个小时才找到分辨率,即,您可以通过在根文件夹(
npm install express-validator@5.3.1-保存精确
答案 5 :(得分:0)
转到package.json,将“ express-validator”:“ ^ 6.6.0”更改为“ express-validator”:“ ^ 5.3.0”,然后手动运行npm i
答案 6 :(得分:0)
这发生在我身上,因为我正在学习过时的(2019 年)教程。如果您安装旧版本(5.3.1 对我有用),它会起作用。我和 Jonathan Wexler 所著的“使用 Node.js 进行编程”一书一起遇到了这个问题。