Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

NodeJS unable to make a GET request asynchronously

NodeJS unable to make a GET request asynchronously

Problem

I am a rookie in Nodejs and asynchronous programming. I am having a problem executing a GET Request inside an asynchronous function. Here I am posting the whole code. I am trying to pull a list of all Urls , add them to a list and send the list for processing to another function.

My problem is with processing them. Inturn for each url I am executing a GET request to fetch the body and to look for image elements in it. I am looking to pass the Image url to a 3rd party api as a GET param. I am unable to execute the GET request as the control doesn't seem to reach there at all.

var async = require("async"),
request = require("request"),
cheerio = require("cheerio");


async.waterfall([

function(callback) {
    var url = "someSourceUrl";
    var linkList = [];
    request(url, function(err, resp, body) {
        var $ = cheerio.load(body);
        $('.list_more li').each(function() {
            //Find all urls and add them to a list
            $(this).find('a').each(function() {
                linkList.push($(this).attr('href'));
            });
        });
        callback(null, linkList);
    });
},


//pass all the links as a list to callback
function(liksListFetched, callback) {
    for (var i in liksListFetched) {
        callback(null, liksListFetched[i]);
    }
}],

//***********My problem is with the below code**************
function(err, curUrl) {
    var cuResp = "";
    console.log("Currently Processing Url : " + curUrl);

    request(curUrl, function(err, resp, body) {

        var $ = cheerio.load(body);
        var article = $("article");
        var articleImage = article.find("figure").children('img').attr('src');
        var responseGrabbed = "API response : ";
        //check if there is an IMG element
        if (articleImage === undefined) {
            console.log("No Image Found.");
            articleImage = 'none';
        }
        else {
            //if there is an img element, pass this image url to an API,
            //So do a GET call by passing imageUrl to the API as a GET param
            request("http://apiurl.tld?imageurl=" + articleImage, function(error, response, resp) {             //code doesn't seem to reach here 
                I would like to grab the response and concatenate it to the responseGrabbed var.
                console.log(resp);
                responseGrabbed += resp;
            });
        }
        console.log(responseGrabbed);// api response never gets concatenated :(
        console.log("_=_=_=_=_=_=__=_=_=_=_=_=__=_=_=_=_=_=__=_=_=_=_=_=_");
        process.exit(0);
    });
});

I appreciate if any one can help me understand the root cause. Thanks in advance.

Problem courtesy of: Eswar Rajesh Pinapala

Solution

request() is asynchronous, so when you're console logging the string, the string hasn't been built yet, you have to do the console log inside the callback :

request("http://apiurl.tld?imageurl=" + articleImage, function(error, response, resp) {                             
    responseGrabbed += resp;
    console.log(responseGrabbed);// api response never gets concatenated :(
    console.log("_=_=_=_=_=_=__=_=_=_=_=_=__=_=_=_=_=_=__=_=_=_=_=_=_");
});

Same goes for terminating the process, which should be done when all the requests have finished

Solution courtesy of: adeneo

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

NodeJS unable to make a GET request asynchronously

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×