简体   繁体   English

Express中的错误处理

[英]Error handling in Express

I continue to receive these errors in JSHint - is it over concern? 我在JSHint中继续收到这些错误-是不是出于关注?

Expected '{' and instead saw 'return'. — column 30

I've read that this is actually an efficient way of handling errors. 我读过,这实际上是处理错误的有效方法。 What are your thoughts? 你怎么看?

app.get("/users", function(req, res, next){

  User.find(function(err, users){

    // an error? get it out of here!
    if (err) { return next(err); }

    // no error? good. I'll do normal stuff here
    // res.render... etc.
  });

});

If you want to avoid this jshint error, you need to include those curly braces around your return statement. 如果要避免此jshint错误,则需要在return语句周围包括那些花括号。 (I'm guessing your real code looks like if (err) return next(err); with no curly braces). (我猜您的实际代码看起来像if (err) return next(err);没有花括号)。

You can also add a trailing comment to the line like so to suppress the warning: 您还可以在行中添加尾随注释,以消除警告:

if (err) return next(err); // jshint ignore:line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM