简体   繁体   English

express-validator 没有收到任何值

[英]express-validator receives no values

I'm using express-validator in a node app.我在节点应用程序中使用 express-validator。 All 4 of my form fields are returning validation errors ("A name is required" and so on).我的所有 4 个表单字段都返回验证错误(“需要名称”等)。 When I console.log the errors variable, all values are blank:当我 console.log 错误变量时,所有值都是空白的:

errors: [{value: '', msg: 'A name is required.', param: 'name', location: 'body'},...

Feedback form:反馈表:

 <form class="feedback-form" method="POST" action="/feedback">
          <div class="form-group">
            <label for="feedback-form-name">Name</label>
            <input
              type="text"
              class="form-control"
              id="feedback-form-name"
              name="name"
              placeholder="Enter your name"
            />
          </div>
          <div class="form-group">
            <label for="feedback-form-email">E-Mail</label>
            <input
              type="text"
              class="form-control"
              id="feedback-form-email"
              name="email"
              placeholder="Enter your email address"
            />
          </div>
          <div class="form-group">
            <label for="feedback-form-title">Title</label>
            <input
              type="text"
              class="form-control"
              id="feedback-form-title"
              name="title"
              placeholder="Title of your feedback"
            />
          </div>
          <div class="form-group">
            <label for="feedback-form-message">Message</label>
            <textarea
              type="text"
              placeholder="Enter your message, then hit the submit button"
              class="form-control"
              name="message"
              id="feedback-form-message"
              rows="6"
            ></textarea>
          </div>
          <button type="submit" class="btn btn-secondary float-right">Submit</button>
        </form>

And my router:还有我的路由器:

router.post(
    "/",
    [
      check("name").trim().isLength({ min: 3 }).escape().withMessage("A name is required."),
      check("email").trim().isEmail().normalizeEmail().withMessage("A valid e-mail is required."),
      check("title").trim().isLength({ min: 3 }).withMessage("A valid title is required."),
      check("message").trim().isLength({ min: 3 }).withMessage("A valid message is required."),
    ],
    (request, response) => {
      const errors = validationResult(request);
      console.log(errors);
      if (!errors.isEmpty()) {
        request.session.feedback = {
          errors: errors.array(),
        };

        return response.redirect("/feedback");
      }
      return response.send("Feedback form posted");
    }
  );
  return router;

Why aren't the form values passing to the router's post method?为什么不将表单值传递给路由器的 post 方法?

You need to access form fields in request.body followed by their respective name as described in THIS post.您需要访问request.body中的表单字段,后跟它们各自的名称,如本文所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM