Created
January 13, 2014 19:22
-
-
Save podkopaev/8406346 to your computer and use it in GitHub Desktop.
The problem is that this function returns an empty array. The array im talking about is hoster.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).