1
const jwt = require('jsonwebtoken');

module.exports = (req, res, next) => {
  try {
    const token = req.headers.authorization;
    const decoded = jwt.verify(token, 'somes');
    req.user = decoded.user;
    next();
  } catch (error) {
    return res.status(401).json({
      message: 'Auth failed',
    });
  }
};

This is my check-auth middle ware I've configured AirBnB coding standards for this expressjs project.

I got an linting error. What does this really mean and how can I get rid of this error? Is it okay to put return statement in front of the next() function call like this return next(); How does that affect to the code?

error Expected to return a value at the end of arrow function consistent-return

1
  • Have you tried adding return next(); Commented May 3, 2019 at 5:35

2 Answers 2

2

It's complaining that one of your code paths is returning a value, but the other isn't. Returning a value isn't necessary here.

Change

return res.status(401).json({
  message: 'Auth failed',
});

to

res.status(401).json({
  message: 'Auth failed',
});
Sign up to request clarification or add additional context in comments.

Comments

1

In my case I had my function that is inside the hook useSelector and I was using it together with the keys

   const petitions = useSelector((store) => {store.cachePetitions.results});

My solution only remove the keys and thus the error generated by eslint disappeared

const petitions = useSelector((store) => store.cachePetitions.results);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.