axios.post()发送数据并在Node.js中的函数中接收数据时出错
我将axios.post()中的数据发送到Node.js中的函数。函数未接受数据的所有参数。 我犯了什么错 这是React中的代码:axios.post()发送数据并在Node.js中的函数中接收数据时出错,node.js,reactjs,mongodb,api,axios,Node.js,Reactjs,Mongodb,Api,Axios,我将axios.post()中的数据发送到Node.js中的函数。函数未接受数据的所有参数。 我犯了什么错 这是React中的代码: axois.post('http://localhost:9000/question/create', **question**) .then((res) => { console.log('res.data -> ', res.data); // The res.dat
axois.post('http://localhost:9000/question/create', **question**)
.then((res) => {
console.log('res.data -> ', res.data); // The res.data sends all the data (see it in the console)
}).catch((error) => {
console.log('error -> ', error);
});
这是Node.js中的代码:
console.log('req -> ', req.body); // The req.body didn't get all the parameters of the data that came to it.
dataQuestion.create(**req.body**, (error, data) => {
if (error) {
return next(error)
} else {
console.log(data)
res.json(data)
}
});
}); ```
Appreciates any answer ...
Thank you!
使用axios执行post时,需要第二个参数,它是post请求的主体
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});