Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6142669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:25:10+00:00 2026-05-23T18:25:10+00:00

I’m using the Facebook graph API to get a user’s friend list and generate

  • 0

I’m using the Facebook graph API to get a user’s friend list and generate a status message based on a template and random friends. If I use alert(response.data[0].name); instead of return(response); my code works and I see the name of the first Facebook friend. When I take it further in my code, it throws an error as undefined. Based on a few hours of trying to work out the problem, I’m thinking I have some kind of issue with scope that doesn’t allow me to use the response any longer or a problem with JSON formatting. I tried playing around with jQuery’s JSON parse as well but didn’t gain anything additional there.

shuffle = function(v){
    for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
    return v;
};

function getFriends() {
    FB.login(function(response) {
        if (response.session && response.perms) {
        FB.api('/me/friends', function(response) {
            return response;
        });
        }
    } , {perms:'publish_stream'});
};

function selectFriend(){
    var friends = getFriends();
    friends = friends.data[0];
    var findex = friends.length
    var randomNumber = (Math.floor( Math.random() * 10 ) % findex);
    return(friends[randomNumber].name);
};

function process(status){
    status = status[0].toString();
    var cindex = status.indexOf("{{friend}}");

    while (cindex != -1){
        status = status.replace("{{friend}}",selectFriend());
        cindex = status.indexOf("{{friend}}");
    }
    return(status);
};

function newStatus(){
    var status = [
        "This status is named {{friend}}",
        "This status loves {{friend}} but likes {{friend}} too.",
        "This status goes by {{friend}} but you can call it {{friend}} if you like.",
        "This status hates {{friend}}"
    ];

    status = shuffle(status);
    $('#status').text(process(status));
}

The code begins with calling newStatus() on click.

EDIT: Here’s how I refactored my code to work after jfriend00’s answer:

shuffle = function(v){
    for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
    return v;
};

function newStatus(){
    var status = [
        "This status is named {{friend}}",
        "This status loves {{friend}} but likes {{friend}} too.",
        "This status goes by {{friend}} but you can call it {{friend}} if you like.",
        "This status hates {{friend}}"
    ];
    status = shuffle(status);
    status = status[0].toString();

    FB.login(function(response) {
        if (response.session && response.perms) {
        FB.api('/me/friends', function(response) {
            var friends = response.data;
            var findex = friends.length

            var cindex = status.indexOf("{{friend}}");

            while (cindex != -1){
                var randomNumber = (Math.floor( Math.random() * 10 ) % findex);
                var name = friends[randomNumber].name;

                status = status.replace("{{friend}}",name);
                cindex = status.indexOf("{{friend}}");
            }
            $('#status').text(status);
        });
        }
    } , {perms:'publish_stream'});
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:25:10+00:00Added an answer on May 23, 2026 at 6:25 pm

    The issue is probably that the calls to the FB api are asychronous. That means when you call FB.api(), it starts an ajax call to FB. Your other code continues to run while that ajax call is being completed. In fact, your call to getFriends finishes and the rest of selectFriend() runs too before the FB ajax call has completed.

    Then, when the FB api call does complete, it calls the completion function (probably where you put an alert). In your code, you aren’t doing anything meaningful in the completion function. You’re just returning the response with

    return response;
    

    But, that isn’t doing anything useful. That return value is not a return from the getFriends() function. It’s a return back to something inside the ajax engine.

    If you want to do something useful with the FB.api() response, you need to run that code or call that code from the completion function specified in the FB.api() call.

    You would have to do something like this pseudo code:

    function getFriends() {
        FB.login(function(response) {
            if (response.session && response.perms) {
            FB.api('/me/friends', function(response) {
                var friends = response.data[0];
                var findex = friends.length
                var randomNumber = (Math.floor( Math.random() * 10 ) % findex);
                var name = friends[randomNumber].name;
                // now continue on with whatever you want to do with the random friend info
                // you cannot just return data from this callback, you have to actually carry
                // out whatever you were going to do with the data here
            });
            }
        } , {perms:'publish_stream'});
    };
    
    function selectFriend(){
        getFriends();
        // the getFriends() function is asynchronous and the FB.api() call has not yet
        // completed so we can't do anything here yet as we have to wait for the
        // FB.api() call to finish first.
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.