Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
Fetch() data is not showing up on the server side

I am sending a simple fetch() to my server as follows:

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);
    });
}

and I can verify that state is populated with the console.log().

However; on the server side req.body is not populated and shows undefined.

// .. 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();
    });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Most likely you need to add a body-parser

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...
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda