Route.get() requires a callback Function but got a [Object Undefined]

Asked

Viewed 7,646 times

-1

In an application nodejs I get this error message:

Version:

Date of publication: 8.9.2

npm: 5.5.1

express: 4.15.5

Error image:

imagem do erro

Code from the app.js

/**
 * Module dependencies.
 */

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

var favicon = require('serve-favicon');
var logger = require('morgan');
var methodOverride = require('method-override');
var session = require('express-session');
var bodyParser = require('body-parser');
var multer = require('multer');
var errorhandler = require('errorhandler')

var app = express();



//load customers route
var customers = require('./routes/customers'); 
var app = express();

var connection  = require('express-myconnection'); 
var mysql = require('mysql');

// all environments
app.set('port', process.env.PORT || 4300);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

var logger = require('morgan');
app.use(logger)

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(methodOverride());

app.use(express.static(path.join(__dirname, 'public')));

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

/*------------------------------------------
    connection peer, register as middleware
    type koneksi : single,pool and request 
-------------------------------------------*/

app.use(

    connection(mysql,{

        host: 'localhost', //'localhost',
        user: 'root',
        password : '',
        port : 3306, //port mysql
        database:'nodejs'

    },'pool') //or single

);





app.get('/', routes.index);
app.get('/customers', customers.list);
app.get('/customers/add', customers.add);
app.post('/customers/add', customers.save);
app.get('/customers/delete/:id', customers.delete_customer);
app.get('/customers/edit/:id', customers.edit);
app.post('/customers/edit/:id',customers.save_edit);


app.use(app.router);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
  • By mistake, user.list is not defined. How you defined this function?

  • not defined.. I will comment on

  • You can’t understand it like that. You use an object that’s not defined, you make an error saying it’s not defined and you don’t know what’s wrong? : t

  • var express = require('express'); var router = express. Router(); /* GET users Listing. */ router.get('/', Function(req, res, next) { res.send('Respond with a Resource'); }); module.Exports = router;

  • this is the user.js code

1 answer

3


Dude, are you exporting the objects that represent the callback functions? I imagine that it is an object or class with the name "customers", only it seems that it is not finding the same. If so, a "module.Exports = customers" in the file that represents these routes would resolve.

  • Hello Thiago, welcome to the SOPT. In this case the ideal is to make a comment, the answer should only be presented if it is an effective solution. Go to "Help" to better understand how to deal with questions and answers here.

  • Hello Thiago. Although your answer is not presented, it was a solution for me. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.