【发布时间】:2020-10-11 16:50:48
【问题描述】:
我有一个简单的表格:
<form method="POST">
<div class="form-group">
<label>Email address</label>
<input type="email" name="email" class="form-control" />
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
我有一个简单的模型:
const User = mongoose.model('User', {
email: {
type: String,
},
password: {
type: String,
}
});
还有一个简单的post方法
.post(async (req, res) => {
console.log(req.body);
const user = new User(req.body);
try {
const saveUser = await user.save();
res.send(saveUser);
} catch (error) {
res.send(error);
}
}
然而,当我在 HTML 文件中提交表单时,我得到一个空对象。我已经尝试了每一个 SO 解决方案,但没有一个对我有用。
注意:我正在使用 JSON 解析器:app.use(express.json());。
请帮忙?
【问题讨论】:
标签: javascript html node.js json