Node.js 快速路线的Redux操作需求主体未定义
我一直在努力使用这个功能,它应该将redux表单值发送到我的express服务器,并等待mongoose模型 到达快速路由后的请求正文不包含我在redux操作中发送的表单值。表单值在客户端(在submitInvestment操作中)正确记录。但是,一旦发送到express(使用axios)->控制台日志,req.body将显示未定义 form.jsNode.js 快速路线的Redux操作需求主体未定义,node.js,reactjs,express,react-redux,redux-form,Node.js,Reactjs,Express,React Redux,Redux Form,我一直在努力使用这个功能,它应该将redux表单值发送到我的express服务器,并等待mongoose模型 到达快速路由后的请求正文不包含我在redux操作中发送的表单值。表单值在客户端(在submitInvestment操作中)正确记录。但是,一旦发送到express(使用axios)->控制台日志,req.body将显示未定义 form.js <form onSubmit={this.props.handleSubmit((values) => { this.props.sub
<form onSubmit={this.props.handleSubmit((values) => { this.props.submitInvestment(values) })}>
export default connect(null, actions)(reduxForm({ form: 'investmentForm' })(InvestmentForm))
route.js
module.exports = app => {
app.post('/api/investments', async (req, res) => {
console.log(req.body)
res.sendStatus(200);
});
}
此外;当我试图用完整构建的route.js捕捉请求时
module.exports = app => {
app.post('/api/investments', async (req, res) => {
const { currency, units, date } = req.body;
const investment = new Investment({
currency,
units,
date,
_user: req.user.id
});
try {
await investment.save();
res.status(200);
} catch (err) {
res.status(422).send(err);
}
});
}
那么我的服务器日志是
[0] (node:25154) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot match against 'undefined' or 'null'.
[0] (node:25154) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
传入的请求需要由中间件处理 App.js
app.use(bodyParser.json());
您是否包括
body解析器
中间件?正如在《仅为验证》中所述,您是否尝试过在服务器上发送带有json.stringify()
和json.parse()
的正文?@Daniele成功了,谢谢!
[0] (node:25154) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot match against 'undefined' or 'null'.
[0] (node:25154) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
app.use(bodyParser.json());