-1

I am using this code:

   function login() {

       FB.login(function(response) {
           if (response.authResponse) {
               console.log('Welcome! Fetching your information.... ');

               FB.api('/me/friends', {
                   fields: 'id,name'
               }, function(response) {
                   for (element in response) {
                       console.log(element.name);
                   }
               });


           } else {
               console.log('User cancelled login or did not fully authorize.');
           }
       }, {
           'scope': 'user_friends'
       });
   }

but I get this response in the console:

Welcome! Fetching your information.... 
2 undefined

Why ?

EDIT:

I have just edited the code above:

   function login() {

       FB.login(function(response) {
           if (response.authResponse) {
               console.log('Welcome! Fetching your information.... ');

               FB.api('/me/friends', {
                   fields: 'id,name'
               }, function(response) {
                   console.log(response.summary);
                   console.log(response);
                   for (element in response.data) {
                       console.log(element.id);
                   }
               });


           } else {
               console.log('User cancelled login or did not fully authorize.');
           }
       }, {
           'scope': 'user_friends'
       });
   }

but now I get this response:

Welcome! Fetching your information.... 
Object {total_count: 58}
Object {data: Array[0], summary: Object}
xRobot
  • 25,579
  • 69
  • 184
  • 304

1 Answers1

1

It is not possible to get all friends anymore. You can only get friends who authorized your App with the user_friends permission too.

This would be the correct code:

FB.api('/me/friends', {
   fields: 'id,name'
}, function(response) {
   for (element in response.data) {
       console.log(element.name);
   }
});

More information: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app

Community
  • 1
  • 1
andyrandy
  • 72,880
  • 8
  • 113
  • 130