TypeError:无法读取属性' 0'未定义的护照-facebook

时间:2015-08-31 14:39:53

标签: node.js facebook facebook-graph-api express passport-facebook

每当我尝试使用Facebook登录时,都会返回此错误,我已按照此链接提示但仍然没有运气。 Passport-facebook doesn't get email

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'] },但仍然没有运气。

1 个答案:

答案 0 :(得分:3)

您是否尝试在配置中明确指定电子邮件字段?在你的情况下应该是:

"overrides": {
  "github:Softdocs/bootstrap@3.3.5": {
    "main": "./dist/js/npm",
    "files": [
      "dist"
    ]
  }
}

注意 profileFields ,似乎是APIv2.4所要求的。 这应该会有所帮助。