NODE.JS - Error: Route.get() requires a callback Function but got a [Object Undefined]

Asked

Viewed 1,437 times

0

In my roters I create:

    var express = require('express');
var router = express.Router();
var service = require('./../services/users');

router.get('/login', service.login);
router.get('/register', service.register);
router.get('/edit/:id', service.edit);

module.exports = router;

Where you have the service:

exports.login = function(req, res) {
    res.render('login');
};

exports.register = function(req, res) {
    res.render('register');
};

but when I spin the npm start gives the error message:

\node_modules\express\lib\router\route.js:202
        throw new Error(msg);
        ^

Error: Route.get() requires a callback function but got a [object Undefined]
    at Route.(anonymous function) [as get] (C:\mean\parking\node_modules\express\lib\router\route.js:202:15)
    at Function.proto.(anonymous function) [as get] (C:\mean\parking\node_modules\express\lib\router\index.js:510:19)
    at Object.<anonymous> (C:\mean\parking\routes\users.js:7:8)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\mean\parking\app.js:9:19)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Ramos\AppData\Roaming\npm-cache\_logs\2018-11-03T16_02_53_765Z-debug.log

** EDITED - MY APP.JS **

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('morgan');
var connection = require('./models');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var cars = require('./routes/cars');


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

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

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

Meu Packarge:

{
  "name": "parking",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "bootstrap": "^4.1.3",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.9",
    "express": "~4.16.0",
    "express-session": "^1.15.6",
    "hbs": "~4.0.1",
    "http-errors": "~1.6.2",
    "mongoose": "^5.3.9",
    "morgan": "~1.9.0"
  }
}

What could I be doing wrong?

1 answer

2


Change

exports.login
exports.register

For

module.exports.login
module.exports.register
  • I had tried it of a doubt Teriror right here of the Stack, but it gives the same error.

  • 1

    Put the rest of the code in that file, something else might be interfering

  • I will edit in more detail.

  • Enter all user service code! Make sure the file path is correct and put the result of console.log(service)

  • I saw that I was missing an Exports.... my mistake. your answer was @Guilherme.

Browser other questions tagged

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