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.
for (var r = 0; r < 100; r++). Wouldn't the last page of your response contain anywhere between 0 and 100 records?