Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

reactjs - Unable to send front end form values to backend api in react, expressjs app?

I am unable to save the data from react form , but in backend I am able to save only mongoose auto generated ID. This API is not saving email and password in database. When I do console log in server it is showing empty object. request body is giving empty object.
My express api route :

 app.post ('/contact', (req, res) => {
      const { email,password } = req.body;
      const contact = new Contact({
       email,
       password, 
      });
      console.log(req.body);
      contact.save();
      res.send(contact); 
    } )

In front end, in browser developer console I can get the data

question from:https://stackoverflow.com/questions/65896360/unable-to-send-front-end-form-values-to-backend-api-in-react-expressjs-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For post data, you will need body-parser

var express = require('express')
var bodyParser = require('body-parser')
var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

More details here http://expressjs.com/en/resources/middleware/body-parser.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...