1

I am working with this Github code. https://github.com/He3556/Mixcloud-Unfollowers

the issue I am facing is that it seems that at the end of the API page, the for loop gives me undefined on the username variable. Ive tried a few fixes but none have worked. here is the loop (please note, varables are declared at the start of the js file, not shown here)

// Get the JSON object - with number of followers and number of following +++++++++++++++++++++++++
$.getJSON('https://api.mixcloud.com/' + uname + '/?metadata=1', function (result) {

            // +++++ Get metadata from Mixloud
            var tfo = (result.follower_count);
            followercnt = parseInt(tfo);
            followingcnt = (result.following_count);

            // Show data
            $("#info").append('Followers:' + followercnt + ' <br />Following:' + followingcnt + '');

            // how often get 100x followers
            if (followercnt == "0") {
                $("#error").append('User was not found on Mixcloud<br /><br /><br /><br />');}
            var tempcnt = (followercnt / 100);
            var cnt = Math.ceil(tempcnt);

            // how often get 100x following
            if (followingcnt == "0") {
            $("#error").append('User was not found on Mixcloud<br /><br /><br /><br />'); }
            var tempcntb = (followingcnt / 100);
            var cntb = Math.ceil(tempcntb);


// ************* START *************
// 1 - get list of followers
getfolls(cnt, uname);

// 2 - get list of ppl you follow
getfollowing(cntb, uname);

});


// Get list of followers +++++++++++++++++++++++++
function getfolls(cnt, uname) {

                  for (var i = 0; i < cnt; i++) {
                          $.getJSON('https://api.mixcloud.com/' + uname + '/followers/?limit=' + lmt + '&offset=' + offst + '', function (result) {
                           //     console.log(JSON.stringify(result));
                                  // +++++ Get 100 packs of followers in the array
                                   var temp = '';
                                    var username = '';
                                   for (var r = 0; r < 100; r++) {
                                     var temp = (result.data[r].username);
                                     if (username == undefined) {
                                        console.log("there was an undefined user!");
                                     }
                                     follers[j] = temp;
                                     j++;
                                     $("#cntfollowers").text('Followers:' + j + ' ');
                                     console.log("temp followers: " + temp);
                               };
                             });
                  offst = (offst + 100);
                 };
  return follers
}

The undefined happens it seems at the end of every page from the API. its not a co-incidence I dont think, however, the rest of the code executes before this can continue and therefore my lists are incomplete. Please help, much appreciated.

4
  • Bit confused with this loop: for (var r = 0; r < 100; r++). Wouldn't the last page of your response contain anywhere between 0 and 100 records? Commented Aug 11, 2024 at 2:20
  • Yes I can't see why it's a problem either. That's why I posted it here. Commented Aug 12, 2024 at 7:24
  • What I mean is, this loop is probably causing the error. If you have less than 100 records in the last response then this will throw an error. Commented Aug 12, 2024 at 21:06
  • yeah i got there in the end, thank you! using a .each and key values sorted it out. the overall code is pants tho. not impressed. Commented Aug 15, 2024 at 18:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.