【问题标题】:Error Expected to return a value at the end of arrow function错误应在箭头函数的末尾返回一个值
【发布时间】:2019-05-03 04:45:07
【问题描述】:
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',
    });
  }
};

这是我为这个 expressjs 项目配置的 AirBnB 编码标准的 check-auth 中间件。

我遇到了一个 linting 错误。这到底是什么意思,我怎样才能摆脱这个错误?像这样return next(); 将 return 语句放在 next() 函数调用前面可以吗?这对代码有什么影响?

error 期望在箭头函数结束时返回一个值 一致返回

【问题讨论】:

  • 你试过加return next();

标签: javascript eslint


【解决方案1】:

它抱怨您的一个代码路径正在返回一个值,但另一个没有。这里不需要返回值。

改变

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

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

【讨论】:

    【解决方案2】:

    在我的情况下,我的函数位于钩子 useSelector 内,我将它与键一起使用

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

    我的解决方案只删除了密钥,因此 eslint 生成的错误消失了

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

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2021-01-15
      • 2021-11-22
      • 2022-11-17
      相关资源
      最近更新 更多