Elamparithi.P
Elamparithi.P

Reputation: 149

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

I have problem including controller file in nodejs router file

my router file has

var express = require('express');
var app = new express.Router();
var ctrl = require('../controller/designer.js');
var renderpages = require('../controller/renderingpages.js')
app.use(express.static('public'));

In designer.js file i have a following structure

var ctrl = 
{

  //controller code
}
module.exports = ctrl

in renderingpages.js file i have a following structure

var renderpages = 
{
  //controller code
}
module.exports = renderpages

i have this issue after including renderingpages.js

Upvotes: 3

Views: 10298

Answers (2)

Clinto Abraham
Clinto Abraham

Reputation: 367

Use exports._function_name = () => {}

// In designer.js file, change into the following structure

exports.ctrl = (req, res, next) =>{
  //controller code
  console.log('running ctrl')
  next()
}


// in renderingpages.js file, change into the following structure


exports.renderpages = (req, res, next) =>{
  //controller code
  console.log('running renderpages')
  next()
}

rather than directly sending an object in form of module.exports = { someFunction, someObject }

Upvotes: 1

matt
matt

Reputation: 1753

Error: Route.get() refers to some line of code that is a get request.

The error means that when your making your get request you are passing an object rather then the expected callback function needed by the request.

What it should look like:

//Format should be '/route', callback
app.get('/iamroute', function(req, res) {
   //callback
});

Upvotes: 3

Related Questions