Route catching an empty error (User.findOne)

Viewed 178

I'm pretty sure this should be a simple thing to fix but I have no idea why its happening, I'm using nodejs and mongodb here.

This is the route I'm calling and when I do that it simply returns 404 with an empty object. I'm sure this is not because it cant find the user with the email adress because userFound will be null in that case. But I couldn't find why its directly catching an empty error or something?

router.post("/notify", function (req, res) {
  User.findOne({ email: req.body.email }).then((userFound) => {
      const notification = {
        type: req.body.type,
        title: req.body.title,
        description: req.body.description,
      };
      notification.destination = userFound;
      Notification.save(function (err, notification) {
        if (err) {
          console.log(err);
          res.status(500).send();
        } else {
          res.send(notification);
        }
      });
    })
    .catch((error) => {
      return res.status(404).json(error);
    });
});

I'm sorry I really feel stupid asking this.. Thanks for the help in advance.

2 Answers

Try to add next in the parameters of your callback function :

router.post("/notify", function (req, res, next) {
  User.findOne(...)
    .catch((error) => next(error));
});

Well first thanks to all of your help guys it turns out that I really did something very stupid, I was actually calling Notification.save() instead of the const name which is notification.save() so that's why i was getting the Notification.save is not a function error

Related