Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!