JavaScript SDK variable not working

I have a fb.api code which generate a id of my friend and now I want to assign that id as a variable but the variable is not working . can any one help me please. Below is the original code(this works alert(friend.id); ):

     FB.api('/me/friends?limit=1', function(response) 
     {
          if(response.data) 
          {
             $.each(response.data,function(index,friend) {

        alert(friend.id);

            });
          } 
          else 
          {
              alert("Error!");
          }
     });

now I edited it like this(but it does not show the user id):

FB.api('/me/friends?limit=1', function(response) {
          if(response.data) {
           $.each(response.data,function(index,friend) {
        var friendid= 'friend.id';
        alert(friendid);
           });
          } 
          else {
              alert("Error!");
          }
      });

Please tell me how can I assign the id to a variable so =I can use that variable where ever I want to.

Upvotes: 0

Views: 68

Answers (2)

Adam Azad
Adam Azad

Reputation: 11297

You problem is that you quoted friend.id which makes it a normal string

FB.api('/me/friends?limit=1', function (response) {
    if (response.data) {
        $.each(response.data, function (index, friend) {
            var friendId = friend.id;
            console.log(friendId);
        });
    } else {
        console.log("An error occurred!");
    }
});

by the ways it makes not sense you limited your call to 1 while you're using .each(); to get the friend id!?

Upvotes: 1

Muhammad Taha
Muhammad Taha

Reputation: 212

you should try this ....

FB.api('/me/friends?limit=1', function(response) {
      if(response.data) {
       $.each(response.data,function(index,friend) {       
    alert(friend.id);
       });
      } 
      else {
          alert("Error!");
      }
  });

Upvotes: 1

Related Questions