gpt4 book ai didi

node.js - Node Route.get() 需要回调函数,但得到了一个 [object undefined]

转载 作者:搜寻专家 更新时间:2023-10-31 23:38:31 26 4
gpt4 key购买 nike

我正在使用 Passport 创建一个 Google OAuth2 身份验证系统。我正在尝试为其编写 Coffeescript 中的路由文件,但出于某种原因我不断收到此错误:

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

我查看了咖啡文件,但没有发现错误。谁能帮帮我?

CoffeeScript 文件:

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

编译后的 JS 文件:

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

最佳答案

你在使用它之前没有设置isLoggedIn,所以它在那个时候仍然是未定义的。

移动这个:

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

在这一行之前:

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

或者删除 isLoggedIn 变量并使用 function isLoggedIn() {} 语法提升函数:

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

关于node.js - Node Route.get() 需要回调函数,但得到了一个 [object undefined],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com