Estoy enviando un fetch() simple a mi servidor de la siguiente manera:
eo.put = function (state) { console.log('PUT:state', state); let _id = encodeURIComponent(state._id); const options = { headers: {'Content-Type': 'application/json'}, method: 'PUT', body: JSON.stringify(state) }; fetch("/articles/put/" + _id, options ) .then((response) => { console.log('RESPONSE', response); // you need to update the DOM here }) .catch((error) => { console.error("DEBUG: fetch/PUT error", error); }); }y puedo verificar que el estado se complete con console.log().
Sin embargo; en el lado del servidor, req.body no está poblado y se muestra indefinido.
// .. snip app.use('/articles', routes.articles); // .. snip // UPDATE OPERATIONS router.route('/put/:_id').put((req, res) => { const _id = req.params._id; const obj = req.body; /* ** ** req.body is empty ** */ debug && console.log('DEBUG: route: /articles/put : req.body', req.body); DBM.updateArticle(_id, obj).then(() => { }).catch((err)=>{ res.status(500).send("DEBUG: database internal error", err); }); res.end(); });Lo más probable es que necesite agregar un analizador de cuerpo
const bodyParser = require('body-parser') // parse application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: false })) // parse application/json app.use(bodyParser.json()) // your routes...