Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

try/catch block not working on express js

I try to create one route using express.js with a try/catch block, but when the conditions in the try block are not met, the code just loads without returning any value

any solution for this problem?

app.post('/test', async (req, res) => {
  try {
    const {name} = req.body;

    if (name === 'name') res.status(200).json({message: 'success!'}) // stuck at there
  } catch (err) {
    res.status(500).json({message: 'failed!'})
  }
})

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are not responding anything to the client in case name !== 'name'

Add an else with a response, or throw an exception that can be caught, and it should work as expected

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading