如何使我的错误消息更加针对注册用户?

时间:2019-01-04 14:11:14

标签: node.js express mean-stack

我目前正在使用MEAN Stack应用程序的后端。用户注册时,我要检查是否填写了特定字段。目前,我已将其设置为提交时将在其中发送json对象的地方。

<!DOCTYPE html>
<html>

<head>
  <title>Page Title</title>
  <link rel="stylesheet" href="https://unpkg.com/simplebar@latest/dist/simplebar.min.css">
</head>

<body>

  <div data-simplebar style="background-color: grey; width: 400px;">
    <h1>My First Heading</h1>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
    <p>My first paragraph.</p>
  </div>
</body>
<script src="https://unpkg.com/simplebar@latest/dist/simplebar.js"></script>

</html>

这是我目前如何进行路线设置后的注册:

{ success: true, msg: "User registered successfully }

我想设置更具体的错误处理。例如,如果没有填写名字,或者基于用户名等原因用户年龄还不够大。

如果您需要更多信息,请告诉我,我会更新我的原始帖子。

1 个答案:

答案 0 :(得分:0)

这个问题真的很广泛;您可以通过多种方式执行此操作,包括简单地编写代码来检查每个字段,为每个字段名称生成唯一的错误消息以及返回JSON中的错误列表;然后可以向用户显示这些错误消息。

如果您正在寻找使用express实现此目的的流行方法,那么express-validator可能就是您想要的。请按照《入门指南》查看它是否适合您的需求。