Gary Sieling

Fixin Express.js error: Route.get() requires callback functions but got a [object Undefined]

If you write a new Express View, you may get an error like so:

Error: Route.get() requires callback functions but got a [object Undefined]
    at Route.(anonymous function) [as get] (/Users/gary/hackathon-starter/node_modules/express/lib/router/route.js:196:15)
    at EventEmitter.app.(anonymous function) [as get] (/Users/gary/hackathon-starter/node_modules/express/lib/application.js:481:19)
    at Object. (/Users/gary/hackathon-starter/app.js:111:5)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:159:18)
    at node.js:444:3

This is an indication that the controller is declaring some export (or maybe not) , and that when you reference it, it is undefined (similar variations include you exporting an object instead of a function).

When you reference the controller you may be doing something like so:

app.get('/schedule', calendarController.getSchedule);

So in order for this to work the controller would need to have a matching function exported:

exports.getSchedule = (req, res) => {
  res.render('schedule', {
    title: 'Schedule A Meeting'
  });
};
Exit mobile version