Node Route.get() requires callback function but go

2020-03-05 03:20发布

问题:

I'm utilizing Passport to create a Google OAuth2 authentication system. I'm trying to write the 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 took a look around the coffee file, and I can't spot the error. Can anyone help me out?

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);

回答1:

You haven't set isLoggedIn before using it, so it is still undefined at that 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 isLoggedIn variable and use function isLoggedIn() {} syntax to have the function hoisted:

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