użyj body-parser
w swoim kodzie ekspresowym dodaj :
global.bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true,
limit: '50mb',
parameterLimit: 100000
}))
app.use(bodyParser.json({
limit: '50mb',
parameterLimit: 100000
}))
app.post('/stored', (req, res) => {
console.log(req.body);
db.collection('quotes').insertOne(req.body, (err, data) => {
if(err) return console.log(err);
res.send(('saved to db: ' + data));
})
});
w twoim frontendzie :
handleSubmit:function(e){
e.preventDefault();
let databody = {
"name": this.state.nameIn,
"quote": this.state.quoteIn
}
fetch('http://localhost:5002/stored', {
method: 'POST',
body: JSON.stringify(databody),
headers: {
'Content-Type': 'application/json'
},
})
.then(res => res.json())
.then(data => console.log(data));
}