I'm trying to make a POST request with my React form component to send my State object as the body, but i'm receiving the body as {}.
My form Component passes the state info to another React Component i made (a bootstrap Input) as prop.
I'm maintaining the info in the Parent's state (where i also do the POST) so this shouldn't be a problem..
My Form Component:
class AgregarReservaForm extends Component {
constructor(props) {
super(props);
this.state = {
titulo: ''
}
this.handleTitulo = this.handleTitulo.bind(this);
}
handleTitulo(event) {
this.setState({titulo: event.target.value})
}
async handleSubmit(event) {
event.preventDefault();
let self = this;
let response = await fetch('http://localhost:3001/reserva', {
method: 'POST',
body: JSON.stringify(self.state),
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
mode: 'no-cors'
});
if(!response.ok) {
throw new Error();
}
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<InputText
name="titulo"
value={this.state.titulo}
onChange={this.handleTitulo}
/>
</form>
My Express POST Route:
app.post('/reserva', (req, res) => {
let body = req.body;
console.log(body);
let nuevaReserva = new Reserva({
titulo: body.titulo
});
nuevaReserva.save((err, reservaDB) => {
if (err) {
return res.status(500).json({
ok: false,
err
});
}
res.json({
ok: true,
reservaDB
});
});
});
Consoloe.log(body) is printing '{}'.