سؤال

I am following this tutorial. when i trying to compile the same code. i got the below error. I can't understood this type of error.

C:\MyPolls>node app.js

C:\MyPolls\node_modules\express\lib\router\index.js:291
   throw new Error(msg);
      ^
Error: .get() requires callback functions but got a [object Undefined]
  at C:\MyPolls\node_modules\express\lib\router\index.js:291:11
  at Array.forEach (native)
  at Router.route (C:\MyPolls\node_modules\express\lib\router\index.js:287:13)

  at Router.(anonymous function) [as get] (C:\MyPolls\node_modules\express\lib
    \router\index.js:318:16)
  at Function.app.(anonymous function) [as get] (C:\MyPolls\node_modules\expre
    ss\lib\application.js:412:26)
  at Object.<anonymous> (C:\MyPolls\app.js:35:5)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)

could you please explain the cause of this error and also i need the solution for avoiding this error in my project.

app.js

/**
 * Module dependencies.
*/

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

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
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('/polls/polls', routes.list);
app.get('/polls/:id', routes.poll);
app.post('/polls', routes.create);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
هل كانت مفيدة؟

المحلول 2

Finally I understood the problem and i have rectified it.The reason for the problem is the below line of code.

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

when i am starting to edit the mean stack project, i have deleted the user.js file in my project directory but i forget to remove the above line of code in my app.js file. Thank you all for your response.

نصائح أخرى

.get() requires callback functions but got a [object Undefined] means that

You have a route defined like app.get('/polls/polls', routes.list);

But routes.list is undefined.

So I guess 1. You haven't added require('routes') inside your file where you define app.get. 2. Or You did not implement the routes.list callback.

Unfortunately I couldn't download the tutorial from the link. Can you put it somewhere like dropbox and share it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top