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

Express密码重置功能开发中req.body.email返回undefined的问题求助

解决req.body.email返回undefined的问题

嘿,我一眼就揪出你遇到的问题根源啦!

你的表单里的邮箱输入框缺少name="email"属性——这就是req.body.email返回undefined的核心原因。body-parser是通过表单元素的name属性来解析请求体数据的,你现在只给了id="email",服务器根本找不到对应字段的提交数据。

修正后的表单代码

给input标签加上name="email"就能解决问题:

<form action="/password_reset" method="POST" >
  <div class="form-group">
    <label for="exampleInputEmail1">Enter your email address</label>
    <input type="email" class="form-control" id="email" name="email" aria-describedby="emailHelp" placeholder="Enter email" required>
  </div>
  <button type="submit" class="btn btn-warning">Submit</button>
</form>

额外小提醒(可选)

虽然你说其他路由运行正常,但可以再确认下Express主文件(比如app.js)里的body-parser配置是否包含urlencoded解析:

const bodyParser = require('body-parser');
// 确保有这一行配置
app.use(bodyParser.urlencoded({ extended: true }));

不过既然其他路由能正常获取req.body,这一步大概率没问题,核心问题还是表单的name属性缺失。

另外,我注意到你路由代码里的嵌套function(token, done)没有被调用,看起来像是async.waterfall流程里的一个步骤?如果后续遇到流程不执行的问题,记得把这个函数放到对应的异步流程里哦。

内容的提问来源于stack exchange,提问作者Emil C.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 04:49:07