Click here to Skip to main content
14,266,036 members
Rate this:
Please Sign up or sign in to vote.
I'm fetching a JSON object from steam and put the steamID's in an array. After that i return the array so i can use it in other functions. But i'm so frustrated that I can't work with the array after i return it.
Also i can't loop through the array.. But when i check console, i can litterally see that there is some array with elements inside but i just can't access it somehow...

function example(){
var friendList = getFriendsOfUser(steamID);
console.log(friendList.length); // returns 0
}

function getFriendsOfUser(steamID){
    var steamFriendIDs = new Array();

    if (steamID == null || steamID == "") {
      url = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key="+_steamAPI+"&steamid=76561197960435530&relationship=friend";
    }
    else {
      url = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key="+_steamAPI+"&steamid="+steamID+"&relationship=friend";
    }

    $.ajax({
      dataType: "json",
      url: url,
      success: function(data){
        $.each(data["friendslist"]["friends"], function(index, value){
          steamFriendIDs.push(data["friendslist"]["friends"][index]["steamid"]);
        });
      }
    });

    return steamFriendIDs;
  }


What I have tried:

Returning just the JSON object doesn't work, because it shows undefined.
The way i did it does give an array with elements inside, but nothing works.

I tried to access it like:
function example(){
var friendList = getFriendsOfUser(steamID);
console.log(friendList[0].length); // cannot read property length of undefined...
}
Posted
Updated 20-Apr-18 8:15am
v4
Rate this:
Please Sign up or sign in to vote.

Solution 1

AJAX stands for Asynchronous Javascript And Xml. The key word there is "Asynchronous" - the $.ajax(...) call returns before the request has been made, so none of the results are available. You are returning an empty array, which will be populated at some point in the future.

You need to re-think how you call the code. Rather than returning the array, you need to return a promise of an array. The code which uses the array needs to be moved to a continuation on that promise.
function getFriendsOfUser(steamID){
    var steamFriendIDs = [];
    var promise = $.Deferred();

    if (steamID === null || steamID === "") {
      url = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key="+_steamAPI+"&steamid=76561197960435530&relationship=friend";
    }
    else {
      url = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key="+_steamAPI+"&steamid="+steamID+"&relationship=friend";
    }

    $.ajax({
      dataType: "json",
      url: url
    }).done(function(data){
        $.each(data["friendslist"]["friends"], function(index, value){
          steamFriendIDs.push(data["friendslist"]["friends"][index]["steamid"]);
        });
        
        promise.resolve(steamFriendIDs);
    }).fail(function(jqXHR, textStatus, errorThrown){
        promise.reject(jqXHR, textStatus, errorThrown);
    });

    return promise.promise();
}

function example(){
    getFriendsOfUser(steamID).done(function(friendList){
        console.log(friendList);
    });
}

Deferred Object | jQuery API Documentation[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Not using your jquery, but the answer is here: AJAX Introduction[^]

You'll note that their AJAX call awaits an answer by checking the readystate.

That allows you to get your results from that return after it is finished on the server side and tells you your reply is ready.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100