Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

What am I doing wrong?

>Solution :

If formData is a JS object, just axios.post('http://localhost:5000/post-entry', formData) is enough.
You can access this object on server side by req.body. But ExpressJS needs a middleware to parse request bodies. You have to use app.use(express.json()) or app.use(bodyParser.json()) where bodyParser is body-parser.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading