[node.js] Route.get() requires callback functions but got a "object Undefined"

I am learning making Todo app. On the website, I am studying is https://coderwall.com/p/4gzjqw/build-a-javascript-todo-app-with-express-jade-and-mongodb

I typed as instruction describes,

var main = require('./routes/main');
var todo = require('./routes/todo');
var todoRouter = express.Router();
app.use('/todos', todoRouter);
app.get('/', main.index);
todoRouter.post('/create', todo.create);
todoRouter.post('/destroy/:id', todo.destroy);
todoRouter.post('/edit/:id', todo.edit);

module.exports ={
  all: function(req, res){
    res.send('All todos');
  viewOne: function(req, res){
    console.log('Viewing '+req.params.id);
  create: function(req, res){
    console.log('Todo created');
  destroy: function(req, res){
    console.log('Todo deleted');
  edit: function(req, res){
    console.log('Todo '+req.params.id+' updated');

and I got this error message

Error: Route.get() requires callback functions but got a [object Undefined]

Did I miss something here?

This question is related to node.js express

The answer is

My suggestion, if you are still using const XXX = require('library or path./') when using module.exports to export multiple functions use an ES6 arrow function

for example:

module.exports = () => {

const  getPosts = (req, res ) =>{
    res.send('THIS WORKS!');

const getPost = async (req, res) => { 
    const { id } = req.params;

    try {
        const post = await PostMessage.findById(id);
    } catch (error) {
        res.status(404).json({ message: error.message });


then Import: const getPosts = require('../controllers/posts.js');

Hope this helps... Cheers! www.miyamotto.net

What happened to me is I was exporting a function like this:

module.exports = () => {
    const method = async (req, res) => {
    return {

but I was calling it like this:

const main = require('./module');


const main = require('./module')();

My problem was a mistake in importing:

I imported my function into the router/index.js like below:

const { index } = require('../controllers');

and used it like this:

router.get('/', index.index);

This was my mistake. I must have used this:

router.get('/', index);

So I changed it to the line above and my problem got solved.

Some time you miss below line. add this router will understand this.

module.exports = router;

check your closing tags in your model, it may be that you have defined a callback in another callback

I got the same error. After debugging, I found that I misspelled the method name that I imported from the controller into the route file. Please check the method name.

This thing also happened with my code, but somehow I solved my problem. I checked my routes folder (where my all endpoints are their). I would recommend you check your routes folder file and check whether you forgot to add your particular router link.

In my case I was trying to 'get' from express app. Instead I had to do SET.

app.set('view engine','pug');

Make sure that


exports.yourFunction = function(a,b){
  //your code



var express = require('express');
var app = express();
var yourModule = require('yourFile');
app.get('/your_path', yourModule.yourFunction);

For me, I ran into this issue when copy pasting a module into another module for testing, needed to change the exports. xxxx at the top of the file

node js and express 4 use this sequences

express = require('express');
var router = express.Router();

module.exports = router;

last line returns this type of error

(1) Make sure that you have imported the corresponding controller file in router file

(2) Make sure that the function name written in the any of the router.get() or router.post() in router.js file is exactly same as the function name written in the corresponding controller file

(3) Make sure that you have written module.exports=router; at the bottom of router.js file

There are two routes for get:

app.get('/', main.index);

Error: Route.get() requires callback functions but got a [object Undefined] This exception is thrown when route.getdoes not get a callback function. As you have defined todo.all in todo.js file, but it is unable to find main.index. That's why it works once you define main.index file later on in the tutorial.

I had the same error. The problem was in the export and import of the modules.

Example of my solution:

Controller (File: posts.js)

exports.getPosts = (req, res) => {
        posts: [
            { tittle: 'First posts' },
            { tittle: 'Second posts' },

Router (File: posts.js)

const express = require('express');
const { getPosts } = require('../controllers/posts');

const routerPosts = express.Router();
routerPosts.get('/', getPosts);

exports.routerPosts = routerPosts;

Main (File: app.js)

const express = require('express');
const morgan = require('morgan');
const dotenv = require('dotenv');
const { routerPosts } = require('./routes/posts');

const app = express();
const port = process.env.PORT || 3000;


// Middleware

app.use('/', routerPosts);

app.listen(port, () => {
    console.log(`A NodeJS API is listining on port: ${port}`);

Running the application (chrome output)

// 20200409002022
// http://localhost:3000/

  "posts": [
      "tittle": "First posts"
      "tittle": "Second posts"

Console Log

jmendoza@jmendoza-ThinkPad-T420:~/IdeaProjects/NodeJS-API-Course/Basic-Node-API$ npm run dev

> [email protected] dev /home/jmendoza/IdeaProjects/NodeJS-API-Course/Basic-Node-API
> nodemon app.js

[nodemon] 2.0.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
A NodeJS API is listining on port: 3000
GET / 304 5.093 ms - -
GET / 304 0.714 ms - -
GET / 304 0.653 ms - -
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
A NodeJS API is listining on port: 3000
GET / 200 4.427 ms - 62
GET / 304 0.783 ms - -
GET / 304 0.642 ms - -

Node Version

jmendoza@jmendoza-ThinkPad-T420:~/IdeaProjects/NodeJS-API-Course/Node-API$ node -v

NPM Version

jmendoza@jmendoza-ThinkPad-T420:~/IdeaProjects/NodeJS-API-Course/Node-API$ npm -v

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to express

UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block jwt check if token expired Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED] npm notice created a lockfile as package-lock.json. You should commit this file Make Axios send cookies in its requests automatically What does body-parser do with express? SyntaxError: Unexpected token function - Async Await Nodejs Route.get() requires callback functions but got a "object Undefined" How to redirect to another page in node.js