如何修复this.validator不是一个函数

how to fix this.validator is not a function

提问人:Taner Özer 提问时间:10/25/2023 更新时间:10/25/2023 访问量:16

问:

validationResult 效果不佳并返回 typeError validatorResult 不是函数,但问题仍然存在

验证器字段.js 文件:

const { validationResult } = require("express-validator");

const validatorFields = (req, res, next) => {
  const errors = validationResult(req);

  if (!errors.isEmpty()) {
    return res.status(400).json(errors);
  }

  next();
};

module.exports = validatorFields;

auth.js 文件:

const { Router } = require("express");
const { check } = require("express-validator");
const routes = Router();
const { validEmail } = require("../helpers/validatorDB.js");
const validateFields = require("../helpers/ValidatorFields.js");
const AuthController = require("../controllers/authController.js");

routes.post(
  "/register",
  [
    check("name", "Name is required").not().isEmpty(),
    check("email", "Invalid email").isEmail(),
    check("password", "Password must have more than 8 characters").isLength({
      min: 8,
    }),
    check("email").custom(validEmail),
    validateFields,
  ],
  AuthController.register
);

routes.post(
  "/login",
  [
    check("email", "Invalid email").isEmail(),
    check("password", "Password is required").not().isEmpty(),
  ],
  AuthController.login
);

module.exports = routes;

返回的JSON数据为:

errors": [
        {
            "type": "field",
            "value": "[email protected]",
            "msg": "this.validator is not a function",
            "path": "email",
            "location": "body"
        }
]

我检查了名为this.validator的缺失或错误定义的函数,但仍然存在相同的问题。我更新了您的 express-validator 版本,但它不起作用。如果错误仍然存在,我使用了当前版本的 express-validator,但它仍然不起作用。我错了吗?任何帮助都非常感谢。“express-validator - req.this.validator 不是函数不是函数”

JSON数据为:

{
    "name":"tanertaner",
    "email": "[email protected]",
    "password":"tanertaner"
}

结果数据:

{
    "errors": [
        {
            "type": "field",
            "value": "[email protected]",
            "msg": "this.validator is not a function",
            "path": "email",
            "location": "body"
        }
    ]
}

和 check(“name”, “名称为必填项”).not().isEmpty(),

TypeError:检查不是函数 \routes\auth.js:11:5) 结果检查是否不为空

但是登录过程成功,我只收到注册错误。

验证 express-validator jsonresult

评论


答: 暂无答案