I have a very simple backend server built in Node/Express that accepts a post request via
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json())
app.post('/auth/login', (req, res) => {
console.log(req.body);
res.send(req.body);
});
When I send a POST to this via Postman with key/value in the body it returns in Postman as expected and I can see in my NodeJS console that the exchange is happening via the console.log. However, when I do the same thing with React, it just logs an empty object.
This is my React:
handleSubmit(e) {
e.preventDefault();
const { email } = this.state;
fetch('/api/login', {
method: 'POST',
headers: {
'Content-Type':'application/json'
},
body: {
"email": email
}
});
console.log("EMAIL", email)
}
I have state being updated as the user types in their email and when they click the submit button it triggers this handleSubmit().
However the output is a lackluster empty {}
I am not sure what I am doing wrong here that the API wouldn't log the state? React logs the data getting posted to the API and it looks valid.
Update
Here is the code that is updating the state:
handleChange(e) {
const email = e.target.value;
this.setState({
email,
});
}
Update, I wasn't using .then() to return the data hence why I was getting an empty object.