简体   繁体   English

TypeError:无法读取未定义的passport-facebook的属性“0”

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

Whenever I try to login using facebook, it will return this error, i have followed this link advise but still no luck. 每当我尝试使用Facebook登录时,它将返回此错误,我已按照此链接提示但仍然没有运气。 Passport-facebook doesn't get email Passport-facebook没有收到电子邮件

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

Here's the code 这是代码

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: "/"
  })
}

I tried to change the scope from { scope: 'email' } to { scope: ['email'] } , still no luck. 我试图将范围从{ scope: 'email' }更改为{ scope: ['email'] } ,但仍然没有运气。

Did you try explicitly specify email field in your config? 您是否尝试在配置中明确指定电子邮件字段? In your case it should be: 在你的情况下,它应该是:

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

Pay attention to profileFields , seems like it's required by APIv2.4. 注意profileFields ,似乎是APIv2.4所要求的。 That should help. 这应该有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取未定义 nodejs 的属性“passport” - TypeError: Cannot read property 'passport' of undefined nodejs 类型错误:无法读取未定义的属性“护照” - typeerror: cannot read property 'passport' of undefined 护照facebook返回“未定义”配置文件对象 - passport-facebook returns 'undefined' profile object 使用护照进行 Facebook 身份验证。 无法读取未定义错误的属性“0” - Facebook authentication with passport. Cannot read property '0' of undefined error Facebook Messenger机器人错误“ TypeError:无法读取未定义的属性'0'。” - Facebook messenger bot error “TypeError: Cannot read property '0' of undefined.” 未定义req.user-节点+快递+护照-facebook - req.user undefined - node + express + passport-facebook Passport-facebook返回除id和displayName之外的未定义字段 - Passport-facebook returns undefined fields except id and displayName 无法运行护照登录策略。 身份验证错误~类型错误:无法读取未定义的属性“身份验证” - Trouble running passport login Strategy. Authentication error~ TypeError: Cannot read property 'authenticate' of undefined 本地护照-无法读取未定义的属性“电子邮件” - Passport local - Cannot read property 'email' of undefined nodejs passport ldapauth“无法读取'未定义的'属性” - nodejs passport ldapauth “Cannot read 'on' property of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM