路由捕获空错误 (User.findOne)
Route catching an empty error (User.findOne)
我很确定这应该很容易解决,但我不知道为什么会这样,我在这里使用 nodejs 和 mongodb。
这是我正在调用的路由,当我这样做时,它只是 returns 404 和一个空对象。
我确定这不是因为它无法找到具有电子邮件地址的用户,因为在这种情况下 userFound 将为空。但是我找不到为什么直接捕获空错误什么的?
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);
});
});
对不起,我真的觉得问这个很愚蠢..提前感谢您的帮助。
尝试在回调函数的参数中添加 next :
router.post("/notify", function (req, res, next) {
User.findOne(...)
.catch((error) => next(error));
});
好吧,首先要感谢你们所有的帮助,事实证明我确实做了一些非常愚蠢的事情,我实际上是在调用 Notification.save()
而不是常量名称 notification.save()
所以这就是为什么我出现 Notification.save is not a function
错误
我很确定这应该很容易解决,但我不知道为什么会这样,我在这里使用 nodejs 和 mongodb。
这是我正在调用的路由,当我这样做时,它只是 returns 404 和一个空对象。 我确定这不是因为它无法找到具有电子邮件地址的用户,因为在这种情况下 userFound 将为空。但是我找不到为什么直接捕获空错误什么的?
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);
});
});
对不起,我真的觉得问这个很愚蠢..提前感谢您的帮助。
尝试在回调函数的参数中添加 next :
router.post("/notify", function (req, res, next) {
User.findOne(...)
.catch((error) => next(error));
});
好吧,首先要感谢你们所有的帮助,事实证明我确实做了一些非常愚蠢的事情,我实际上是在调用 Notification.save()
而不是常量名称 notification.save()
所以这就是为什么我出现 Notification.save is not a function
错误