首页 > 解决方案 > 无法将表单数据发布到 app.js

问题描述

我知道已经问过类似的问题,但我找不到任何适合我的东西。

我正在尝试将一些 HTML 表单数据发布到我的 app.js 文件中。

这是我的 index.handlebars 文件:

<form method = 'post' action = '/refund'>
  <input type = "text" name = "transId" placeholder = "transID">
  <input type = "text" name = "amount" placeholder = "amount">
<input type = "submit">

这发布到以下路线:

//refund route
app.post('/refund', (req, res, next) => {

console.log(req.body.amount);
console.log(req.body.transId);

但是,我收到错误:TypeError: Cannot read property 'amount' of undefined

我已经尝试了每一种组合,只是传递req.body,,req.params一切都是公正的undefined

标签: htmlnode.jsexpresshandlebars.js

解决方案


将此中间件添加到您的代码中

app.use(bodyParser.urlencoded({ extended: false }));

因为您正在发送form数据。

//myapp.herokuapp.com/refund另外,尝试提供路由的完整 URL 路径,action 例如<form>.


推荐阅读