简体   繁体   中英

Error Expected to return a value at the end of arrow function

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

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',
});

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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