Travis Cunningham
Travis Cunningham

Reputation: 51

Error: Route.get() , can't seem to get past

Keep getting this error, whilst trying to run my app. Low level of knowledge have tried many solutions online to fix this, but nothing has got me up and running. Here is the Error-

    Error: Route.get() requires a callback function but got a [object Undefined]
    at Route.(anonymous function) [as get] (d:\dev_portal\bit_racer_real\alpha_bit_racer\node_modules\express\lib\router\route.js:202:15)

index.js

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


    router.get('/', function(req, res) {
    res.redirect('/catalog');
    });

    module.exports = router;

catalog.js

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

 // Require controller modules.

var horse_controller = require('../controllers/horseController');

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


module.exports = router;

app.js

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var catalogRouter = require('./routes/catalog'); 

var app = express();

var mongoose = require('mongoose');
var mongoDB = 'mongodb://admin:[email protected]:23753/alpha_bit_racer';
mongoose.connect(mongoDB);
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));


app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/catalog', catalogRouter);





module.exports = app;

Not exactly sure where I have gone wrong, I have also heard that I may have a call in my routes index.js file in express. The call doesn't exist. I have spent hours going over the code making small changes, trying to get this simple error to clear :(

Has anyone else experienced this problem. Id love to get a clear answer.

Upvotes: 3

Views: 57

Answers (4)

Travis Cunningham
Travis Cunningham

Reputation: 51

I got this resolved.

I was missing .index function from controller .js file.

added this line

exports.index = function(req, res) { res.send('NOT IMPLEMENTED: Site Home Page'); };

now .index is no longer looking for an object.

Thanks for the help, appreciate the responses. :)

Upvotes: 0

Yves Kipondo
Yves Kipondo

Reputation: 5603

In your catalog.js you require your controller lke this require('../controllers/horseController') that means you have horseController.js file or horseController is a directory and in which you have an index.js file. After requiring it you save it in horse_controller variable and when you register It as an request handler you specify horse_controller.index

This presume you have exporting in your horseController an object like this

module.export = {
    index: function(request, response) {
        // Your code goes here
    }
}

but if it isn't the case that why your are getting that error

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

This code means you have heighter you are exporting nothing in your ../controllers/horseController file or ../controllers/horseController/index.js

If the index.js file under the horseController directory you can pass the horse_controller variable directly as an request handler to the app.get like this

app.get('/', horse_controller);

Passing it directly because in the index.js file you are exporting the Express Router directly

Upvotes: 1

Daksh M.
Daksh M.

Reputation: 4817

as far as I can see, your index.js file is in the horsecontroller directory.

But when you use CommonJS's require with a directory instead of a file, it automatically defaults that to ../path/to/directory/index.js, so you don't have to specify horse_controller.index in the app.get function, instead just specify horse_controller.

The final code would look like this:

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

 // Require controller modules.

var horse_controller = require('../controllers/horseController');

router.get('/', horse_controller);


module.exports = router;

Upvotes: 2

Marcos Pereira
Marcos Pereira

Reputation: 1176

Like Daksh said, in the line router.get('/', horse_controller.index); (file catalog.js) horse_controller.index is not a function.

Upvotes: 1

Related Questions