node.jserror-handlingsoftware-quality

Conflict between code quality check and implicit next() in Express error handling


I am starting with NodeJS. In error handling, the following code declares the next callback and does not use it:

app.get('/user/:id', async function (req, res, next) {
  var user = await getUserById(req.params.id)
  res.send(user)
})

because next(value) is implicit from Express 5:

Starting with Express 5, route handlers and middleware that return a Promise will call next(value) automatically when they reject or throw an error.

This shortcut is in conflict with code quality checks. When I run ESLint on the script, I get:

server/app.js
  95:26  error  'next' is defined but never used  no-unused-vars

What is the appropriate way to get the same functionality and ensure code quality: remove next from the function's arguments? Add next() at the end? Or disable the rule on code checking?


Solution

  • You are not using next(). so just remove this one.

    app.get('/user/:id', async function (req, res) {
      var user = await getUserById(req.params.id);
      res.send(user);`
    })