简体   繁体   English

express.js,如果我删除下一个参数,错误处理程序将不起作用

[英]express.js, error handler doesn't work if I remove next parameter

I'm learning Express.js and using their generator I generated a new app with:我正在学习Express.js并使用他们的生成器生成了一个新应用程序:

npm install express-generator -g && express myapp

After that I saw in app.js that there is this code:之后我在app.js看到有这样的代码:

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render("error");
});

在此处输入图片说明

As you can see in the image eslint is complaining about next parameter , declared and never used.正如您在图像中看到的, eslint抱怨next parameter ,已声明但从未使用过。 I agree, but if I remove it express doesn't render the error page.我同意,但如果我删除它, express不会呈现错误页面。

Why this behavior?为什么会有这种行为?

This is dangerous for me because I can't trust anymore eslint or my coding degree?这对我来说很危险,因为我不能再相信 eslint 或我的编码学位? I'm surely missing something.我肯定错过了一些东西。 But what ?但是什么

Because the error handler in express is determined by the number of parameters:因为 express 中的错误处理程序是由参数数量决定的:

  (function (req, res, next) { }).length // 3
  (function (err, req, res, next) {}).length // 4
  (function (err, req, res) {}).length // 3

therefore if you remove the parameter the error handler won't be treated as an error handler anymore.因此,如果您删除该参数,则错误处理程序将不再被视为错误处理程序。

This is dangerous for me because I can't trust anymore eslint or my coding degree?这对我来说很危险,因为我不能再相信 eslint 或我的编码学位?

If linters would understand the code better than a programmer ... why would we need programmers?如果 linters 比程序员更能理解代码……为什么我们还需要程序员?

This is known issue with ESlint and yet they still did not solve that issue.这是 ESlint 的已知问题,但他们仍然没有解决这个问题。 ESlint does not give error if one of the res, req is not used but gives in error handler where you are required to have 4 arguments.如果未使用 res、req 之一,ESlint 不会给出错误,但会在错误处理程序中给出,您需要有 4 个参数。 There are 2 solutions, one is to disable ESlint for the next line for this error like so:有两种解决方案,一种是针对此错误在下一行禁用 ESlint,如下所示:

// eslint-disable-next-line no-unused-vars
app.use((error, req, res, next) => {

and another one i to just use next() at the end of the error handler which will never be executed after you either render or send json when error occurs.另一个我只在错误处理程序的末尾使用next() ,在发生错误时渲染或发送 json 后永远不会执行。

next() is necessary for handling async errors. next()是处理异步错误所必需的。 Add:添加:

  if (err) {
     next(err);
  }

Eslint requires you to use all the parameters that are passed to the function. Eslint 要求您使用传递给函数的所有参数。

For your case you have two options, execute next () at the end of your function or include this same function among eslint comments:对于您的情况,您有两个选择,在函数末尾执行 next () 或在 eslint 注释中包含相同的函数:

/ * eslint-disable * / / * eslint-enable * / /* eslint-disable * / / * eslint-enable * /

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

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