theob
theob

Reputation: 309

Cannot read property of 'get' of undefined, express middleware

Apologies for the title in advance, I really do not know how to summarize this.

I'm getting the following error while trying to run my Express app

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 is a function that looks something like this:

export function authHandler (req, res, next) {
  var token = req.get('Authorization')
  //shortened
  next()
}

It's being called in application.mjs from an Express router like this:

router.use(authHandler()) (yes, it's imported)

But, for some reason, req is showing as undefined.

Any ideas?

Upvotes: 0

Views: 981

Answers (1)

Josh
Josh

Reputation: 669

You are calling your handler and registering its result to the router. What you actually want to do is register the handler itself: router.use(authHandler)

Edit: The reason for your error is that you call authHandler without any arguments. Anyway, you don't need to call it at all.

Upvotes: 4

Related Questions