Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript {key:";}和{key:";},json文件之间的区别?_Javascript_Json_Express_Middleware_Express Validator - Fatal编程技术网

Javascript {key:";}和{key:";},json文件之间的区别?

Javascript {key:";}和{key:";},json文件之间的区别?,javascript,json,express,middleware,express-validator,Javascript,Json,Express,Middleware,Express Validator,我正在尝试在我的快速路由器上实现验证,问题是当我传递{title:}时,快速验证程序没有抛出错误,但当我传递{title:}时,它会工作 exports.postValidatorCheck = [ check("title", "The title must not we empty").isEmpty(), check("title", "The Length of the Title should be in greater then 10").isLength({ min

我正在尝试在我的快速路由器上实现验证,问题是当我传递{title:}时,快速验证程序没有抛出错误,但当我传递{title:}时,它会工作

exports.postValidatorCheck = [
  check("title", "The title must not we empty").isEmpty(),
  check("title", "The Length of the Title should be in greater then 10").isLength({
    min: 10,
    max: 1500
  }),
  function(req, res, next) {
    let errors = validationResult(req);
    console.log(errors);
    if (!errors.isEmpty()) {
      const firstError = errors.errors.map(err => err.msg)[0];
      return res.status(400).json({ error: firstError });
    }
    next();
  }
];
jSON文件:

{
"title":"",
"body":"This is a new Post"
} 

无误

JSON文件

{
"title":" ",
"body":"This is new post"
}
错误如预期

首先,
是一个空字符串<代码>“”不是;它包含一个空格字符。如果要将任何空格计算为空,应使用


至于您的实际问题,您测试的是
isEmpty()
,而您应该测试的是
not()。isEmpty()

验证应使用负数:

check("title", "The title must not we empty").not().isEmpty()
这将确保
标题
不是空的,这是我认为您想要的。

因此,当我传递{title:}时,express validator应该抛出一个错误,但它没有,它验证标题并继续。