Why doesn't this POST ever resolve and return a 200 code even though the database does get updated?

Issue

I am new to web development and trying to write a post request in express with mongodb as the database and cannot figure out why this posts to the database (the document is created) though it does not return with a 200 status code.

Here’s the function, it logs the site but the http request just sends forever.

exports.site_create = async (req, res) => {
  const site = new Site({
    name: req.body.name,
    location: req.body.location,
    well_type: req.body.well_type,
  })

  try {
    const siteToSave = await site.save()
    res.status(200)
    console.log(siteToSave)  
  }

  catch(error) {
    res.status(400).json({message: error.message})
  }
};

Solution

in the try block, You have just setup the status_code but, you haven’t sent any response.

Solution:

res.status(200).json({site: siteToSave});

Answered By – Ahmed Elhady

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published