0

每當我嘗試使用Facebook登錄時,它將返回此錯誤,我已按照此鏈接建議,但仍然沒有運氣。 Passport-facebook doesn't get emailTypeError:無法讀取未定義護照的屬性'0'Facebook

newUser.facebook.email = profile.emails[0].value; 
             ^
TypeError: Cannot read property '0' of undefined  

下面的代碼

var passport = require('passport'); 
var FacebookStrategy = require('passport-facebook').Strategy; 
var User = require('../models/user'); 

var facebookConfig = { 
    clientID: 'ID', 
    clientSecret: 'SECRET', 
    callbackURL: 'http://localhost:8080/facebook/callback' 
}; 

var facebookInit = function(token, refreshToken, profile, callback) { 
    User.findOne({ "facebook.id": profile.id }, function(err, user) { 
    if (err) return callback(err); 

    if (user) { 
     return callback(null, user); 
    } 

    var newUser = new User(); 
    newUser.facebook.id = profile.id; 
    newUser.facebook.token = token; 
    newUser.facebook.email = profile.emails[0].value; 
    newUser.facebook.displayName = profile.displayName; 
    newUser.facebook.photo = profile.photos[0].value 

    newUser.save(function(err) { 
     if (err) { 
     throw err; 
     } 
     return callback(null, newUser); 
    }); 
    }); 
} 

passport.use(new FacebookStrategy(facebookConfig, facebookInit)); 

passport.serializeUser(function(user, callback) { 
    callback(null, user.id) 
}); 

passport.deserializeUser(function(id, callback) { 
    User.findById(id, function(err, user) { 
    callback(err, user); 
    }); 
}); 

module.exports = { 
    facebookLogin: passport.authenticate("facebook", { scope: ['email'] }), 
    facebookCallback: passport.authenticate("facebook", { 
    successRedirect: "/profile", 
    failureRedirect: "/" 
    }) 
} 

我試圖從{ scope: 'email' }範圍更改爲{ scope: ['email'] },仍然沒有運氣。

回答

3

您是否嘗試在配置中明確指定電子郵件字段?你的情況應該是:

var facebookConfig = { 
    clientID: 'ID', 
    clientSecret: 'SECRET', 
    callbackURL: 'http://localhost:8080/facebook/callback', 
    profileFields: ['id', 'email', 'gender'] 
}; 

注重profileFields,就像它是由APIv2.4要求似乎。 這應該有所幫助。

相關問題