Search code examples
javascriptfacebookfacebook-graph-apiphonegap-pluginsphonegap-build

Facebook email is returned as undefined from phonegap facebookConnect plugin


I am using the phonegap facebook Connect plugin to enable facebook login in my app. However the facebook email is being returned as undefined.

Do I need to add something into my code? I have looked up this issue on the internet and it seems my code should work. Everything else is returned except for the email address.

I would appreciate if you can help

Here is my javascript code:

   facebookConnectPlugin.api('/me?fields=id, email, link, name, picture', ["public_profile"],function(data){
        var fb_user_id = data.id;
        var fb_email = data.email;
        var fb_name = data.name;
        var fb_picture_url = data.picture.data.url;
        var fb_user_link = data.link;
        alert("fb_email" + fb_email);
    }); //end api call

Edit:

I tried a test user account with this code and the email address DID get returned. However for the real account I was testing with this doesn't work.

With more testing I tried adding in the email permission as follows however this did not work as the data that I got back stated "FACEBOOK_NON_JSON_RESULT"

 facebookConnectPlugin.api('/me?fields=id, email, link, name, picture', ["public_profile", "email"],function(data){
            var fb_user_id = data.id;
            var fb_email = data.email;
            var fb_name = data.name;
            var fb_picture_url = data.picture.data.url;
            var fb_user_link = data.link;
            alert("fb_email" + fb_email);
        }); //end api call

Solution

  • I find a workaround for this problem which was to do two separate api requests as follows:

        facebookConnectPlugin.api('/me?fields=email', ["email"], function(apiResponse) {
    
                //alert("api" + JSON.stringify(apiResponse));
                fb_email = apiResponse.email;
                alert("fb_email" +fb_email); //email being retrieved successfully
                facebookConnectPlugin.api('/me?fields=id, name, link, picture', ["public_profile"],function(data) {
                     alert("data" + JSON.stringify(data));
    
                     var fb_user_id = data.id;
                     var fb_name = data.name;
                     var fb_picture_url = data.picture.data.url;
                     var fb_user_link = data.link;
    
                     alert("fb_user_id" + fb_user_id);          
                     alert("fb_name" + fb_name);
                     alert("fb_picture_url" + fb_picture_url);
                     alert("fb_user_link" + fb_user_link);
                     //do stuff with facebook user data here
                 }
    
                 ,function(error){  
                       //api call failed
                       alert("api call Failed: " + JSON.stringify(error));
                 }); //end api                          
       }
    
       ,function(error){    
    
                alert("email api call Failed: " + JSON.stringify(error));
       }); //end api    
    

    This works perfect!