On Jan 13, 2014, at 13:12, Denis Podkopaev wrote:
> i can't find my error. Every time i run this function it returns an empty
> array.
> The array im talking is about is hoster.
> I hope someone can help me.
>
> 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');
> }
> });
> }
You’re not thinking asynchronously.
The order of operations in your code is:
1. Clear the hoster array
2. Do some other stuff, then run request() and tell it to call an anonymous
function when it’s done requesting
3. Run the original callback() and pass it the hoster array (which is still
empty at this point)
4. Some time later, request() will complete and call its anonymous callback,
which will push an item onto the hoster array, which is no longer relevant
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.