You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js CRUD应用更新异常:req.body获取值为undefined

问题排查与修复方案

核心问题1:表单字段名与后端接收字段不匹配

你的HTML表单中输入框的name属性和后端req.body里获取的字段完全不一致,这是导致所有值都为undefined的直接原因:

  • 表单里的caliPush1Id → 后端用req.body.id
  • 表单里的caliPush1Ex → 后端用req.body.exercice
  • 表单里的caliPush1Sets → 后端用req.body.sets
  • 表单里的caliPush1Reps → 后端用req.body.repetitions
  • 表单里的caliPush1Kgs → 后端用req.body.kilograms

修改后的update.ejs表单代码:

<div class="div_table_update">
    <table class="table">
        <form method="POST" action="/updateData">
            <tr>
                <td>
                    <input type="text" name="id" placeholder="id" class="workout_data_input" value="<%= calipplpush1[0].id %>" readonly/>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="exercice" placeholder="exercice" class="workout_data_input" value="<%= calipplpush1[0].exercice %>"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="sets" placeholder="sets" class="workout_data_input" value="<%= calipplpush1[0].sets %>"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="repetitions" placeholder="reps" class="workout_data_input" value="<%= calipplpush1[0].repetitions %>"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="kilograms" placeholder="kgs" class="workout_data_input" value="<%= calipplpush1[0].kilograms %>"/>
                    <input type="submit" name="submit_button" class="workout_data_btn" value="ok" />
                </td>
            </tr>
        </form>
    </table>
</div>

注:顺便修复了HTML结构错误,原代码中第一个td未包裹在tr标签内,可能导致表单渲染异常。

核心问题2:SQL更新语句的参数顺序错误

你的SQL更新语句占位符顺序为exercice=?, sets=?, repetitions=?, kilograms=? where id=?,但传入的参数数组顺序是[id,exercice,sets,repetitions,kilograms],两者完全不匹配,就算拿到正确数据也会更新错误字段。

修改后的server.js POST接口代码:

// Post Table Update to the database
app.post('/updateData', function(req, res){
    var id = req.body.id;
    var exercice = req.body.exercice;
    var sets = req.body.sets;
    var repetitions = req.body.repetitions;
    var kilograms = req.body.kilograms;
    console.log(id, exercice, sets, repetitions, kilograms);
    // 修正参数顺序:先填set的字段,最后填where的id
    var sql=`update calipplpush1 set exercice=?, sets=?, repetitions=?, kilograms=? where id=?`
    con.query(sql,[exercice, sets, repetitions, kilograms, id], function(error,result){
        if (error) {
            console.log(error);
            return; // 出错时停止后续执行
        }
        console.log("Data Updated");
        res.redirect('/cali-ppl')
    })
})

额外检查:确保配置了POST请求体解析中间件

Node.js/Express默认不会解析POST请求的表单数据,需在server.js所有路由之前添加以下中间件:

// 解析application/x-www-form-urlencoded格式的表单数据
app.use(express.urlencoded({ extended: true }));
// 如需解析JSON格式数据可补充
app.use(express.json());

若缺少此配置,req.body会一直为undefined,这是新手常见错误。

验证步骤

  1. 重启Node.js服务
  2. 进入更新页面修改数据并提交
  3. 查看控制台输出,确认能看到正确字段值
  4. 检查MySQL表calipplpush1,确认数据已更新

内容的提问来源于stack exchange,提问作者shxdo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 04:47:17