Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.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 express validator如何将req.validationErrors()放到前端?_Javascript_Express_Express Validator - Fatal编程技术网

Javascript express validator如何将req.validationErrors()放到前端?

Javascript express validator如何将req.validationErrors()放到前端?,javascript,express,express-validator,Javascript,Express,Express Validator,您好,谢谢您的阅读。我尝试使用快速验证器。例如,如果名称输入为空,它可以正常工作并阻止帖子。但是我不知道怎样才能从正面得到错误信息。我尝试过很多事情,但都没有成功。希望有人能帮上忙,并对留言的大小表示歉意。这里有一个指向express validator文档的链接,如果有帮助的话() 我的代码。。。 我创建了一个基本表单: <form id="sign-in"> <div class="form-group"> <label for="

您好,谢谢您的阅读。我尝试使用快速验证器。例如,如果名称输入为空,它可以正常工作并阻止帖子。但是我不知道怎样才能从正面得到错误信息。我尝试过很多事情,但都没有成功。希望有人能帮上忙,并对留言的大小表示歉意。这里有一个指向express validator文档的链接,如果有帮助的话()

我的代码。。。 我创建了一个基本表单:

 <form id="sign-in">
      <div class="form-group">
        <label for="nom">name</label>
        <input id="name" type="text" class="form-control" placeholder="name" autocomplete="family-name">
      </div>

      <div class="form-group">
        <label for="username">username</label>
        <input id="username" type="text" class="form-control" placeholder="username" autocomplete="given-name">
      </div>


      <div class="form-group">
        <label for="email">email</label>
        <input id="email" class="form-control" type="email" placeholder="monemail@gmail.com" autocomplete="email"></input>
      </div>

      <div class="form-group">
        <label for="password">password</label>
        <input id="password" class="form-control" type="password" autocomplete="current-password"></input>
      </div>

      <div class="form-group">
        <label for="confirm-password">confirm-password</label>
        <input id="confirm-password" class="form-control" type="password" autocomplete="current-password"></input>
      </div>

      <button type="submit" class="btn btn-primary">sign-in</button>
    </form>
</section>


<footer>
</footer>
<script type="module" src="/js/sign-in.js"></script> 
})

最后是服务器端:

app.post('/sign-in', (req, res, next) => {
// Start express validator //
  req.checkBody('name', 'Please write your name').notEmpty()
  req.checkBody('username', 'Please write your name').notEmpty()
  ...

  const errors = req.validationErrors()

    if (errors) {
      SEND ME BACK MY ERROR MESSAGE
    } else {
      this part works fine, thank you
    }  
  }) 

根据客户端的设置方式,此处有两个选项:

  • 使用或将错误发送回视图。请参阅以了解差异
  • 使用
    res.json
    发回从
    validationErrors()
    创建的
    errors
    对象。还要确保设置适当的HTTP状态

  • 当我找到解决方案后,我发布了它,它可能对其他人有用。 以下是sign-in.js文件:

    .then(res => {
      if (res.status !== 200) {
        res.json()
        .then(res => errorsMessages.innerHTML = res.map(e => `<p>${e.msg}</p>`).join(''))
      } else {
        window.parent.location.reload()
      }
    })
    

    谢谢Francisco的帮助。

    谢谢Francisco,我想使用第二种解决方案。我曾尝试在服务器文件中添加此项:return res.status(422).json({errors:errors}),但它不起作用,可能是因为我不知道在sign-in.js文件上写什么。您需要调整表单。类似于通过AJAX提交表单并适当处理响应。
    .then(res => {
      if (res.status !== 200) {
        res.json()
        .then(res => errorsMessages.innerHTML = res.map(e => `<p>${e.msg}</p>`).join(''))
      } else {
        window.parent.location.reload()
      }
    })
    
    const errors = req.validationErrors()
      if (errors) {
        res.status(422).json(errors)
      } else {...}