passport-facebook icon indicating copy to clipboard operation
passport-facebook copied to clipboard

keep getting TypeError: Cannot read property '0' of undefined for email

Open arashdoescode opened this issue 9 years ago • 14 comments

This is the error that I'm getting currently newUser.facebook.email = profile.emails[0].value; ^ TypeError: Cannot read property '0' of undefined

Take a look at the code. (I'm using facebook version v 2.4) https://gist.github.com/arashdoescode/89634453360a8e426690

Probably it is an obvious mistake, hopefully someone experience could point me where is the problem

Cheers!

arashdoescode avatar Sep 02 '15 22:09 arashdoescode

You do have a misspelling but I don't think you'd be able to compile so I doubt that's the problem -- apssport. Try using profileFields as ['emails']. I am using emails and it is working. Also you will want to include displayName since I see you are using that.

adam-beck avatar Sep 04 '15 04:09 adam-beck

@adam-beck @jaredhanson It is working! for some account but for other accounts it shows the same error newUser.facebook.email = profile.emails[0].value; ^ TypeError: Cannot read property '0' of undefined

Am i missing something here?

arashdoescode avatar Sep 04 '15 06:09 arashdoescode

@adam-beck @jaredhanson Here is the new file https://gist.github.com/arashdoescode/165a5ca1c4bfdc98f9cd

arashdoescode avatar Sep 04 '15 06:09 arashdoescode

@adam-beck @jaredhanson some other account email is hidden on facebook, would that be the cause? I just checked that my other account that could authorize , its email is hidden as well. I've been testing this alot of times but seems like there is no luck

Account A - keep showing the same error -->TypeError: Cannot read property '0' of undefined

Account B - Does fetch everything including the email

What did i miss here?

arashdoescode avatar Sep 04 '15 06:09 arashdoescode

What is your profileFields?

profileFields: ['id', 'name', 'displayName', 'picture.type(large)', 'hometown', 'profileUrl', 'email']

pateljatin avatar Sep 04 '15 20:09 pateljatin

@pateljatin check out this link https://gist.github.com/arashdoescode/165a5ca1c4bfdc98f9cd this is my profileFields

arashdoescode avatar Sep 05 '15 06:09 arashdoescode

Yep - try "email".

profileFields: ['email']

and then when you access it in your code after auth...use it like so:

email: profile.emails[0].value || null

pateljatin avatar Sep 05 '15 19:09 pateljatin

@pateljatin the weird thing is there are some accounts that works but others don't

I have two facebook accounts to test this out

  1. First Account does fetched the email
  2. Second Account return the same error over and over again

I'm not really sure what is issue over here.

arashdoescode avatar Sep 05 '15 20:09 arashdoescode

in the second account, has user verified their email? facebook will not return email if user hasn't verified their email address.

pateljatin avatar Sep 08 '15 01:09 pateljatin

Then what will it return? The earlier comment said use emails in profile field you are saying use email what is correct one?

SarasArya avatar Nov 21 '15 10:11 SarasArya

@SarasArya you can use either one although I think for this module it is preferred to use emails.

The profile fields get mapped here

adam-beck avatar Nov 23 '15 04:11 adam-beck

@adam-beck I had not confirmed my email id.Hence I wasn't getting it. Thanks for the help.

SarasArya avatar Nov 24 '15 05:11 SarasArya

Firstly, set your profileFields section.

'facebookAuth' : {
        'clientID'      : 'your App ID',
        'clientSecret'  : 'your App Secret',
        'callbackURL'   : 'your Callback URL',
        'profileFields'   : ['emails']
    }

Now, we need to pull these details wherever we configure our passport code.

passport.use(new FacebookStrategy({

        // pull in our app id and secret from our auth.js file
        clientID        : configAuth.facebookAuth.clientID,
        clientSecret    : configAuth.facebookAuth.clientSecret,
        callbackURL     : configAuth.facebookAuth.callbackURL,
        profileFields   : configAuth.facebookAuth.profileFields

    }

Janusz0 avatar Feb 08 '17 05:02 Janusz0

Thanks

deep-dot avatar Sep 05 '21 20:09 deep-dot