2016-02-10 73 views
0

當我執行主應用程序以及passport-facebook模塊時,出現以下錯誤。我再次嘗試npm install passport-facebook,但沒有變化。我需要幫助來解決這個問題。 authenticate()護照模塊的預定義方法?護照驗證錯誤

C:\codebag\app\routes.js:53 
app.get('/api/auth/facebook', passport.authenticate('facebook', { scope : 'email' })); 
            ^

TypeError: Cannot read property 'authenticate' of undefined 
    at module.exports (C:\codebag\app\routes.js:53:43) 
    at Object.<anonymous> (C:\codebag\server.js:49:24) 
    at Module._compile (module.js:413:34) 
    at Object.Module._extensions..js (module.js:422:10) 
    at Module.load (module.js:357:32) 
    at Function.Module._load (module.js:314:12) 
    at Function.Module.runMain (module.js:447:10) 
    at startup (node.js:139:18) 
    at node.js:999:3 

routes.js

var express = require('express') 
//var router = express.Router(); 
module.exports = function(app,passport) { 
    app.get('*', function(req, res) { 
     res.sendfile('./public/index.html'); 
    }); 

    // route for facebook authentication and login 
    app.get('/api/auth/facebook', passport.authenticate('facebook', { scope : 'email' })); 

    // handle the callback after facebook has authenticated the user 
    app.get('/api/auth/facebook/callback', passport.authenticate('facebook', { 
     successRedirect : '/profile', 
     failureRedirect : '/' 
    })); 

    // route for logging out 
    app.get('/api/logout', function(req, res) { 
     req.logout(); 
     res.redirect('/'); 
    }); 
}; 

// route middleware to make sure a user is logged in 
function isLoggedIn(req, res, next) { 
    // if user is authenticated in the session, carry on 
    if (req.isAuthenticated()) 
     return next(); 

    // if they aren't redirect them to the home page 
    res.redirect('/'); 
} 

server.js

// modules ================================================= 
var express  = require('express'); 
var app   = express(); 
var mongoose  = require('mongoose'); 
var bodyParser  = require('body-parser'); 
var methodOverride = require('method-override'); 
var db = require('./config/db'); 
var passport = require('passport'); 
var port = process.env.PORT || 8080; 
mongoose.connect(db.url); 
app.use(bodyParser.json()); 
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json 
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded 
app.use(passport.initialize()); 
app.use(passport.session()); 
app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT 
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users 

// routes ================================================== 
require('./app/routes')(app); // pass our application into our routes 

// start app =============================================== 
app.listen(port); 
console.log('Magic happens on port ' + port);   // shoutout to the user 
exports = module.exports = app;       // expose app 

回答

0

您不需要護照在​​router.js,沒有它,變量是undefined

一下添加到

var passport = require('passport'); 
+0

其實我試過了,但我得到同樣的錯誤,我重新安裝護照和護照,Facebook,但沒有改變。 –

1

在server.js文件我已經通過了「應用」單獨使用「要求(」 ./應用/路線)routes.js的routes.js的開始(app)「,所以我編輯了這條語句」require('./app/ routes')(app,passport)「。 現在它的工作。謝謝