Not found error in node course 2nd project in 8th section 7th lecture

Hi there

if anyone are facing the error that when the genre id is not correct and u wanna sho some response status and can’t send message of “Not found”

then replace this code:


router.get('/:id', async (req, res) => {

    const genre = await Genre.findById(req.params.id);

    if (!genre) return res.status(404).send('The genre with the given ID was not found.');

    res.send(genre);
});

with this code :


router.get('/:id', async (req, res) => {

  try {

    const genre = await Genre.findById(req.params.id);
    res.send(genre);

  } catch (err) {
    res.status(404).send("The genre with the given ID was not found.");
  };

});
1 Like