简体   繁体   English

无法从我的客户端(react.js)获取数据(node.js)

[英]Not able to get data (node.js) from my client (react.js)

I'm not able to retrieve the data I'm sending from the client to the server.我无法检索从客户端发送到服务器的数据。

My react code is the following:我的反应代码如下:

axios.post('http://localhost:5000/post-entry', { data: formData })
.then(res => {
  console.log(res);
})

My server code is the following:我的服务器代码如下:

app.post('/post-entry', (req, res) => {
  console.log(req.data, "res.body.data here");
});

When it reaches the post on my server, the log is undefined .当它到达我服务器上的帖子时,日志是undefined

What am I doing wrong?我究竟做错了什么?

If formData is a JS object, just axios.post('http://localhost:5000/post-entry', formData) is enough.如果formData是 JS 对象,只需axios.post('http://localhost:5000/post-entry', formData)就足够了。 You can access this object on server side by req.body .您可以通过req.body在服务器端访问此对象。 But ExpressJS needs a middleware to parse request bodies.但是 ExpressJS 需要一个中间件来解析请求体。 You have to use app.use(express.json()) or app.use(bodyParser.json()) where bodyParser is body-parser .您必须使用app.use(express.json())app.use(bodyParser.json())其中 bodyParser 是body-parser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将React.js组件从node.js发送到客户端? - How could I send a React.js component from my node.js to my client? 从 Node.JS 服务器应用程序中发出 GET 请求并将其发送到 React.JS 客户端 - Make a GET request from within a Node.JS server app and send it to React.JS client 服务器端和客户端端的路由-React.js和Node.js - Routes on server & client side - React.js & Node.js 在不从客户端调用的情况下运行 Node.js function(React.js) - Run Node.js function without calling from client side(React.js) 在 React.js + Node.js 中从浏览器地址栏调用 GET api 时如何不显示返回的数据? - How to not show returned data when GET api called from browser address bar in React.js + Node.js? 在 React.js 中尝试从 Node.js 获取数据时出现 404 错误服务器正在运行时 - Get 404 error When trying to fetch data from Node.js in React.js While server is running MySQL由HTML中的node.js生成,由react.js生成 - MySQL result from node.js in html, by react.js React.Js & Node.js 登录无响应 - React.Js & Node.js No response from login 无法使用GET调用从node.js获得对react.js的响应 - Cannot get response from node.js to react.js using GET call React.js使用axios发送整数数据,但在Node.js中获取[object object] - React.js Using axios to send integer data but get [object object] in Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM