0

I'm trying to retreive the email of a user logging into my web app; here's my code:

  FB.api('/me', function(response) {
    console.log(response.name + ', ' + response.email);
  });

the user name is correct, but email results as

undefined

Where am I going wrong?

andyrandy
  • 72,880
  • 8
  • 113
  • 130
Lorenzo Tosone
  • 393
  • 3
  • 10

1 Answers1

4

https://developers.facebook.com/docs/reference/javascript/FB.login/v3.2

Ask for the email permission in the login process:

FB.login((response) => {
  // handle the response
}, {scope: 'email'});

Also, you need to ask for the fields you want to get:

FB.api('/me', {fields: 'name,email'}, (response) => {
    console.log(response.name + ', ' + response.email);
});

Make sure the user even has an Email, it´s not required. And make sure you actually get asked for the email permission in the login popup.

Side Note: I would just use console.log(response), so you can see the whole object instead of some undefined values.

andyrandy
  • 72,880
  • 8
  • 113
  • 130