Express验证程序错误:expressValidator不是函数 [英] Express Validator Error: expressValidator is not a function

查看:241
本文介绍了Express验证程序错误:expressValidator不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安装和使用 express-validator 软件包.我已经安装了软件包版本(6.0.0),然后在server.js文件中,代码是:

I'm trying to install and use express-validator package. I've installed the package version (6.0.0) and then in my server.js file the code is:

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')

然后在下面几行中输入:

Then a few lines down I've put in the following:

const app = express()
const documentationApp = express()
app.use(expressValidator())

服务器(使用nodemon)重新加载更改时,应用程序崩溃并显示:

When the server reloads the changes (using nodemon) the app crashes and says:

TypeError:expressValidator不是函数

server.js文件中还有其他代码,但是我已经删除了大部分与我无关的代码.

There are other bits of code in my server.js file but I've stripped out most of it that isn't relevant I think.

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')
  }
})

推荐答案

//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"})
  }

})

这篇关于Express验证程序错误:expressValidator不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆