没有得到任何回复 - Express Validator

时间:2018-03-30 07:17:32

标签: node.js validation express express-validator

我正在使用最新版本的express-validator进行验证。 我没有收到任何回复,但旧方法,checkBody工作正常,而新方法,check('keyName')无法正常工作。

以下是我的代码。

的package.json

"express-validator": "^5.0.3",

routes.js

var authValidator = require('./../validation/auth.validation');
var routes = require('express').Router();

routes.post('/login', [
  authValidator.validateLogin, 
  authValidator.checkValidationResult ], function (req, res) {
     console.log('3');
     //res.send("Some other stuffs");
  }
);

module.exports = routes;

auth.validation.js

module.exports.validateLogin = validateLogin;
module.exports.checkValidationResult = checkValidationResult;

const {check, validationResult} = require('express-validator/check');
const {matchedData, sanitize} = require('express-validator/filter');

var response = require('./../general/MyResponse');
var messages = require('./../general/messages');

function validateLogin(req, res, next) {
  console.log('1');
  return [
    check('email').isLength({min: 1}).withMessage(messages.EMAIL_REQUIRED)
    .isEmail().withMessage(messages.INVALID_EMAIL),
    check('password').isLength({min: 1}).withMessage(messages.PASSWORD_REQUIRED),
  ]
}

function checkValidationResult(req, res, next) {
  console.log('2');
  var result = validationResult(req)
  if (!result.isEmpty()) {
    response.createResponse(
      res, 400,
      result.array()[0].msg,
      {'error': result.array()[0].msg}, {}
    )
  } else {
    next()
  }
}

我注意到节点js无法继续validateLogin中的auth.validation.js函数。

任何人都可以告诉我上面的代码有什么问题。

在控制台内部,只显示1个。

我正在附加screenShot以进行报复。

screenShot of error

2 个答案:

答案 0 :(得分:1)

validateLogincheckValidationResult正在作为中间件应用于您的路线。在中间件中,您使用next()方法调用队列中的下一个中间件。就像你的checkValidationResult一样。

validateLogin的情况下,它没有将控制传递给下一个中间件。但check v5中的express-validator方法本身就是一种中间件方法。因此,我想它将无法正常工作。

请查看:https://github.com/ctavan/express-validator/issues/449

尝试使用以下代码:

<强> routes.js

var authValidator = require('./../validation/auth.validation');
var routes = require('express').Router();
var authValidations = authValidator.getAuthValidations();

routes.post('/login', 
  authValidations,
  authValidator.checkValidationResult, function (req, res) {
     console.log('3');
     //res.send("Some other stuffs");
  }
);

module.exports = routes;

<强> auth.validations.js

module.exports.getAuthValidations = getAuthValidations;
module.exports.checkValidationResult = checkValidationResult;

const {check, validationResult} = require('express-validator/check');
const {matchedData, sanitize} = require('express-validator/filter');

var response = require('./../general/MyResponse');
var messages = require('./../general/messages');

function getAuthValidations(req, res, next) {
  return [
    check('email').isLength({min: 1}).withMessage(messages.EMAIL_REQUIRED)
    .isEmail().withMessage(messages.INVALID_EMAIL),
    check('password').isLength({min: 1}).withMessage(messages.PASSWORD_REQUIRED),
  ]
}

function checkValidationResult(req, res, next) {
  console.log('2');
  var result = validationResult(req)
  if (!result.isEmpty()) {
    response.createResponse(
      res, 400,
      result.array()[0].msg,
      {'error': result.array()[0].msg}, {}
    )
  } else {
    next()
  }
}

答案 1 :(得分:0)

我们需要使用简单的数组,而不需要创建函数。

请点击此链接 Is it possible to do the validation in a separate file and not inline in the route? - GitHub了解更多详情。

代码应该是这样的。

<强> auth.validation.js

var response = require('./../general/MyResponse');
var messages = require('./../general/messages');

const {check, validationResult} = require('express-validator/check');
const {matchedData, sanitize} = require('express-validator/filter');

module.exports.validateLogin = [
    check('email').isLength({min: 1}).withMessage(messages.EMAIL_REQUIRED).isEmail().withMessage(messages.INVALID_EMAIL),
    check('password').isLength({ min: 1 }).withMessage(messages.PASSWORD_REQUIRED),
];

module.exports.checkValidationResult = checkValidationResult;

function checkValidationResult(req, res, next) {
    console.log('2');
    var result = validationResult(req)
    if (!result.isEmpty()) {
        response.createResponse(res, 400,
            result.array()[0].msg,
            {'error': result.array()[0].msg}, {}
        )
    } else {
        next()
    }
}

`