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

313
Visualizações
Getting a 400 error when trying to fetch a post request

I'm trying to use the fetch api for a post request to the database. I am making a simple discussion platform and am trying to allow for the user to submit a form that gets saved in mongo.

This is my route in my handlers.js file:

router.post('/add', async (req, res) => {
  console.log('inside post method')
  const userComment = { userid: "testUser" + stringify(Math.random()), commentid: "comment" + stringify(Math.random()), content: req.body, reply: false }
  //const userComment = req.body
  const newComment = new commentModel(userComment)
  try {
    await newComment.save(async (err, newCommentResult) => {
      console.log('New comment created!')
      res.end('New commentcreated!')
    })
  } catch (err) {
    console.log(err)
    res.end('Comment not added!')
  }
})

This is my fetch in my front end:

function addComment (content) {
    console.log(content)
    //**** NOT DONE , NEW COMMENT ADD INTO DATABASE */
    //!!!!! Call api request here !!!!//
    const type = {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(content),
      mode: 'cors'

    }
     fetch('/add', type)
    .then(
      console.log('success')
    )
    .catch(
      error => console.log('error', error)
    )
    
  }

I am basically trying to submit data via a form that gets saved in mongo.

about 4 years ago · Santiago Gelvez
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