无法从快速表单中读取数据(返回null)

问题描述 投票:0回答:1

无法从快速表单中读取数据(返回null),我在server.js中使用了body解析器

//Parser
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json());

这是路由器方法

router.post('/', async (req, res) => {
    console.log('customer');
    const { error } = validate(req.body);
    console.log(req.body); 
    if (error) return res.status(400).send(error.details[0].message);

    let customer = new Customer({
        name: req.body.name,
        mobile: req.body.mobile,
        isGold: req.body.isGold
    })
    console.log(customer);
    customer = await customer.save();

    res.send(customer);
});

这是html表单,我有3个字段(名称,移动,isGold): -

 <form action="/api/customer" method="POST">
    <div class="form-row">
      <div class="form-group col-md-6">
        <label for="name">Full Name</label>
        <input type="name" class="form-control" id="name" placeholder="name">
      </div>   
    </div>
    <div class="form-row">

      <div class="form-group col-md-6">
        <label for="mobile">Mobile</label>
        <input type="mobile" class="form-control" 
        id="mobile" placeholder="Enter your Mobile">
      </div>
    </div>
 
    <div class="form-group">
      <div class="form-check">
        <input class="form-check-input" type="checkbox" id="isGold">
        <label class="form-check-label" for="gridCheck">
          Is Gold
        </label>
      </div>
    </div>
     <button type="submit" class="btn btn-primary">Save</button>
  </form>
node.js express
1个回答
0
投票

您需要将name属性添加到输入字段。

<div class="form-group col-md-6">
        <label for="mobile">Mobile</label>
        <input type="mobile" class="form-control" 
        id="mobile" name="mobile" placeholder="Enter your Mobile">
      </div>
</div>

通常GET / POST请求以key:value pair的形式发送.Here key是表单的name属性。

例如:http://example.com/page?parameter=value&also=another

这里“参数”(&“也”)是表单输入字段名称。

© www.soinside.com 2019 - 2024. All rights reserved.