Wednesday, 15 March 2017

Cannot read property 'get' of undefined. Using nodejs on Cloud9

If this helps at all: I am following the instructions in chapter 3 of Simon Holmes' book, "Getting MEAN with Mongo, Express, Angular, and Node".

I am in the beginning stages of separating my routes and controllers into separate files. When I bypass creating the variables and hard code " require('express').Router().get('/', ctrlMain.index); " and other combinations that have the same functionality, I get the same error. So, I have narrowed the problem down to the first line of index.js, although the error I am getting points to the GET method in line 6 of the same file. I have provided my index.js, main,js, app.js, files, as well as the full error message I'm getting below. I would be happy to provide any other parts of my project/file structure if this information isn't enough! Hopefully someone will be able to point me in the right direction.

This is my index.js file:

var express = require('express');
var router = express.Router();
var ctrlMain = require('../controllers/main');

/* GET home page. */
router.get('/', ctrlMain.index);

module.exports = router;

My main.js file:

/* GET home page */
module.exports.index = function(req, res) {
    res.render('index', { title: 'Express' });
};

and my app.js file (not sure if this is helpful):

var express = require('express')
  , routes = require('./app_server/routes/index')
  , user = require('./app_server/routes/user')
  , http = require('http')
  , path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/app_server' + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

Finally, here is the error I'm getting:

TypeError: Cannot read property 'use' of undefined at Object.<anonymous> (/home/ubuntu/workspace/lab5/app_server/routes/index.js:6:7) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object.<anonymous> (/home/ubuntu/workspace/lab5/app.js:6:14) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10)



via Eve Gottwald

No comments:

Post a Comment