【发布时间】:2021-09-26 05:02:13
【问题描述】:
在 app.js 中,我有
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
所以如果我请求一些不存在的 url,比如http://localhost/notfound,上面的代码就会执行。
在http://localhost/posts/:postId这样的现有网址中,我想在访问一些不存在的postId或删除的postId时抛出404错误。
Posts.findOne({_id: req.params.id, deleted: false}).exec()
.then(function(post) {
if(!post) {
// How to throw a 404 error, so code can jump to above 404 catch?
}
【问题讨论】:
-
这个
Posts.findOne是在页面请求中调用还是在页面请求中调用promise?
标签: javascript node.js express