Node The Route.get () function requires a callback function, but received [an undefined object]

I am using Passport to create a Google OAuth2 authentication system. I am trying to write route files in Coffeescript for it, except for some reason I keep getting this error:

D:\Programming\weebly-site\node_modules\express\lib\router\route.js:162
            throw new Error(msg);
                  ^
    Error: Route.get() requires callback functions but got a [object Undefined]
      at D:\Programming\weebly-site\node_modules\express\lib\router\route.js:162:15
      at Array.forEach (native)
      at Route.(anonymous function) [as get] (D:\Programming\weebly-site\node_module
    s\express\lib\router\route.js:158:15)
      at Function.proto.(anonymous function) [as get] (D:\Programming\weebly-site\no
    de_modules\express\lib\router\index.js:490:19)
      at Object.<anonymous> (D:\Programming\weebly-site\routes\admin.js:15:10)
      at Object.<anonymous> (D:\Programming\weebly-site\routes\admin.js:37:4)
      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)
      at Module.require (module.js:364:17)
      at require (module.js:380:17)
      at Object.<anonymous> (D:\Programming\weebly-site\app.js:16:19)
      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)
      at Module.require (module.js:364:17)
      at require (module.js:380:17)
      at Object.<anonymous> (D:\Programming\weebly-site\bin\www:3:11)
      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)
      at Function.Module.runMain (module.js:497:10)
      at startup (node.js:119:16)
      at node.js:906:3

      

I have looked at the coffee file and I do not see the error. Can anyone help me?

Coffeescript file:

express = require('express')
passport = require('../config/passport.js')
router = express.Router()

router.get '/', (req, res) ->
    res.render 'admin/admin_index.jade'

router.get '/editor', isLoggedIn, (req, res) ->
    res.render 'admin/admin_editor.jade'

router.get '/auth/google', passport.authenticate('google', {scope:['profile', 'email']})

router.get '/auth/google/callback', passport.authenticate('google', {successRedirect: '/editor', failureRedirect: '/'})

isLoggedIn = (req, res, next) ->
    return next() if req.isAuthenticated()

    res.redirect '/'


module.exports = router

      

Compiled JS file:

// Generated by CoffeeScript 1.8.0
(function() {
  var express, isLoggedIn, passport, router;

  express = require('express');

  passport = require('../config/passport.js');

  router = express.Router();

  router.get('/', function(req, res) {
    return res.render('admin/admin_index.jade');
  });

  router.get('/editor', isLoggedIn, function(req, res) {
    return res.render('admin/admin_editor.jade');
  });

  router.get('/auth/google', passport.authenticate('google', {
    scope: ['profile', 'email']
  }));

  router.get('/auth/google/callback', passport.authenticate('google', {
    successRedirect: '/editor',
    failureRedirect: '/'
  }));

  isLoggedIn = function(req, res, next) {
    if (req.isAuthenticated()) {
      return next();
    }
    return res.redirect('/');
  };

  module.exports = router;

}).call(this);

      

+3


source to share


1 answer


You haven't installed isLoggedIn

it before using it, so it's still undefined at this point.

Move this:

isLoggedIn = function(req, res, next) {
  if (req.isAuthenticated()) {
    return next();
  }
  return res.redirect('/');
};

      

before this line:



router.get('/editor', isLoggedIn, function(req, res) {

      

Or get rid of the variable isLoggedIn

and use the syntax function isLoggedIn() {}

to support the function:

function isLoggedIn(req, res, next) {
  if (req.isAuthenticated()) {
    return next();
  }
  return res.redirect('/');
};

      

+5


source







All Articles