Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

The problem is that this function returns an empty array. The array im talking about is hoster.

View gist:8406346
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
portal.getHosterByStream = function(stream, callback) {
request(URL + stream, function (error, response, body) {
if (!error && response.statusCode === 200) {
var hoster = [];
var $ = cheerio.load(body);
var information = $('body div[id=frmMain] div[id=dontbeevil] div[id=Vadda]').html();
var items_hoster = $(information).find("ul[id=HosterList]").html();
$(items_hoster).each(function (i, item) {
var rel = $(item).attr('rel');
 
if (rel != undefined) {
request(MIRROR + rel, function (error, response, body) {
if (!error && response.statusCode === 200) {
var host = JSON.parse(body);
 
var href = "";
var positionHref = 9;
var i = 0;
while (host.Stream.substr(positionHref + i, 1) != '"') {
href = util.format('%s%s', href, host.Stream.substr(positionHref + i, 1));
 
i++;
}
hoster.push(href);
} else {
console.log('error second request');
}
});
}
});
callback(hoster);
} else {
console.log('error request page');
}
});
}

Not tested, but i think i got this problem before.
The each at Line 9 returns imidiately, so the callback Line 33 get executed before the array is filed.
You might have a look at array filter/map (and/or take the nodeschool.io functional programming lesson).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.