【问题标题】:Route.get() requires a callback function but got a [object Object]Route.get() 需要一个回调函数但得到一个 [object Object]
【发布时间】:2021-03-31 01:28:52
【问题描述】:

我正在使用 MERN 应用程序。

MERN?

  • MongoDB:数据库
  • Express:中间件
  • React.js:前端
  • Node.js:后端

但是在进行令牌身份验证时出现以下错误:

Route.get() requires a callback function but got a [object Object]

这是我的快递代码

router.get('/', userTokenAuth,async (req, res) => {
    try {
        const user = await User.findById(req.user.id).select('-password');
        res.json({
            data: user
        })
    } catch (error) {
        res.status(500).json({
            error
        })
    }
})

这是验证码


require('dotenv/config');
const jwt = require("jsonwebtoken");

module.export = (req, res, next) => {
    const header = req.header('token');
    if(header){
        const decoded = jwt.verify(
            token,
            process.env.TOKEN,
            (err, user) => {
                if(err) return res.sendStatus(403);                
                console.log(decoded)
                req.user = decoded.user
                next();
            }
        )
    }else{
        res.status(404);
    }
}

【问题讨论】:

标签: node.js express middleware jwt-auth


【解决方案1】:

原因可能是你的 auth 函数导出不正确。

尝试将module.export 更改为module.exports

【讨论】:

  • 是的......但我仍然没有得到任何回应。它只是不停地发送请求。
  • 这可能是另一个问题。为它打开一个新的 SO 问题。
猜你喜欢
  • 2021-11-28
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多