Reputation: 9002
Here is what I am working with, I have simplified it a little:
if (response.authResponse) {
FB.api('/me', function(response) {
response.id;
user_id = response.id; // I thought I could at least access it below if it was defined globally...
});
var friend_list = [];
FB.api('/me/friends', function(response) {
$.each(response.data,function(index,friend) {
friend_list.push(friend.id);
});
user_friend_list = friend_list.toString();
});
alert("user id:"+user_id+"friend list:"+user_friend_list); // Here is where I would like these two variables to show up
} else {
//user cancelled login or did not grant authorization
}
probably for some obvious reason (due to my poor javascript and jquery) I cannot capture the variables. If you provide an answer please explain a little so I can gain insight and learn.
Upvotes: 0
Views: 2521
Reputation: 55248
Try this.
if (response.authResponse) {
FB.api('/me', function(data) {
var user_id = data.id;
FB.api('/me/friends', function(response) {
var friend_list = [];
for (i = 0; i < response.data.length; i++) {
friend_list.push(response.data[i].id);
}
//user_friend_list = friend_list.join(",");
alert("user id:" + user_id + "friend list:" + friend_list.join(","));
});
});
}
else {
alert("no access granted");
}
Altered bits and pieces of code. Comment if you have a problem with this solution.
Upvotes: 1
Reputation: 946
The only place you can assume these values are defined is in the callback functions provided as second arguments. This is bescause the api() method is asynchronous.
You may end with some code like :
if (response.authResponse) {
FB.api('/me', function (response) {
// Now first asyncrhonous call is over, let's do the second one
response.id;
user_id = response.id; // I thought I could at least access it below if it was defined globally...
var friend_list = [];
FB.api('/me/friends', function (response) {
// Now second asynchronous call is over, we have both values defined.
$.each(response.data, function (index, friend) {
friend_list.push(friend.id);
});
user_friend_list = friend_list.toString();
alert("user id:" + user_id + "friend list:" + user_friend_list); // Here is where I would like these two variables to show up
});
});
} else {
//user cancelled login or did not grant authorization
}
Upvotes: 0