Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 无法读取';获取';未定义的、快速的中间件_Node.js_Express - Fatal编程技术网

Node.js 无法读取';获取';未定义的、快速的中间件

Node.js 无法读取';获取';未定义的、快速的中间件,node.js,express,Node.js,Express,提前为标题道歉,我真的不知道如何总结这一点 我在尝试运行我的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

提前为标题道歉,我真的不知道如何总结这一点

我在尝试运行我的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
中被快速路由器调用,如下所示:

export function authHandler (req, res, next) {
  var token = req.get('Authorization')
  //shortened
  next()
}
路由器。使用(authHandler())
(是,它已导入)

但是,由于某些原因,
req
显示为未定义


有什么想法吗?

您正在呼叫处理程序并将其结果注册到路由器。 您实际上要做的是注册处理程序本身:
router.use(authHandler)


编辑:出现错误的原因是调用
authHandler
时没有任何参数。无论如何,您根本不需要调用它。

您正在调用处理程序并将其结果注册到路由器。 您实际上要做的是注册处理程序本身:
router.use(authHandler)


编辑:出现错误的原因是调用
authHandler
时没有任何参数。无论如何,您根本不需要调用它。

那么,您是否向函数传递了
req,res
?如代码,> AuthHuffer-Req(Req,Re..])<代码>路由器。使用(AuthHoall())< /COD>似乎是错误的,如果<代码>路由器< /代码>是我所想的,请考虑尝试<代码> App.使用(AuthHoall())< /C> >如果<代码> App是您的快件instance@SumanKundu哦,是的,答案是有道理的,你立即调用了这个函数,没有任何理由,您是否将
req,res
传递给函数?如代码,> AuthHuffer-Req(Req,Re..])<代码>路由器。使用(AuthHoall())< /COD>似乎是错误的,如果<代码>路由器< /代码>是我所想的,请考虑尝试<代码> App.使用(AuthHoall())< /C> >如果<代码> App是您的快件instance@SumanKundu哦,是的,答案是有道理的,你立即调用了这个函数,没有任何问题,因为它是在没有参数的情况下被调用的,
req
是未定义的,最终导致错误。你是对的。我将更新我的答案,因为这在技术上是错误的。此外,因为它在没有参数的情况下被调用,
req
未定义,最终导致错误。你是对的。我会更新我的答案,因为这在技术上是错误的。