Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...

JavaScript
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:
JavaScript
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

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.
JavaScript
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[^]
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900