My code worked well when I give him true user Id but I want to run my if statement when I pass a wrong Id but it it always go for catch block error

Viewed 15

My code worked well when I give him true user Id but I want to run my if statement when I pass a wrong Id but it it always go for catch block error.

 router.get("/users/:id", (req, res) => {
      const _id = req.params.id;
      User.findById(_id)
        .then((result) => {
          if (!result) {
            res.status(404); 
          } else res.send(result);
        })
        .catch((e) => {
          res.status(400).send(e);
        });enter code here
    });
0 Answers
Related