提前对标题表示歉意,我真的不知道如何总结。
我在尝试运行 Express 应用程序时收到以下错误
TypeError: Cannot read property 'get' of undefined
at authHandler (file:///api/src/authUtil.mjs:34:19)
at file:////api/src/routes/application.mjs:6:12
at ModuleJob.run (internal/modules/esm/module_job.js:96:12)
authHandler
是一个如下所示的函数:
export function authHandler (req, res, next) {
var token = req.get('Authorization')
//shortened
next()
}
它是在 application.mjs
中从 Express 路由器中调用的,如下所示:
router.use(authHandler())
(是的,它已导入)
但是,由于某种原因,req
显示为未定义。
有什么想法吗?
最佳答案
您正在调用处理程序并将其结果注册到路由器。
您真正想要做的是注册处理程序本身:router.use(authHandler)
编辑:错误的原因是您在不带任何参数的情况下调用 authHandler
。不管怎样,你根本不需要调用它。
关于node.js - 无法读取未定义的 Express 中间件的 'get' 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52246756/