簡體   English   中英

TypeError:無法讀取未定義的passport-facebook的屬性“0”

[英]TypeError: Cannot read property '0' of undefined passport-facebook

每當我嘗試使用Facebook登錄時,它將返回此錯誤,我已按照此鏈接提示但仍然沒有運氣。 Passport-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'] } ,但仍然沒有運氣。

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

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

注意profileFields ,似乎是APIv2.4所要求的。 這應該有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM