1

i can't find the bug in this function. It returns every time an empty array. Hope someone can help me.

https://gist.github.com/podkopaev/8406346

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');
        }
    });
}

1 Answer 1

2

request() is async, so you're calling callback(hoster) before any of your hoster.push(href) calls get a chance to execute.

So change your code to not call callback(hoster) until all of your request() callbacks have completed.

UPDATE

In your latest gist, you're calling callback(null, 103); before your request calls have called their callbacks and populated hoster.

Instead of rel.forEach, use async.eachSeries in a pattern like:

async.eachSeries(rel, function(item, cb) {
        request(MIRROR + item, function (error, response, body) {
            if (!error && response.statusCode === 200) {
                ...
                hoster.push(href);
            }
            cb();
        });
    }, function(err) {
        // This won't get called until all the request callbacks are done.
        callback(null, 103);
    }
);
Sign up to request clarification or add additional context in comments.

5 Comments

could you please help me with the code. i am new to node.js and trying to fix this bug for a week now.
@user3197431 Has my answer helped you understand the problem with your code?
Yeah it helped me understanding the problem. I tried to find a workaround with the async library but it didn't work to me.
@user3197431 Edit your question to show how you tried to solve this with async and hopefully we can tell you what you're doing wrong.
Me again. It doesn't work. This is my current code. gist.github.com/podkopaev/8632141 The variable hoster is still empty.

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.