noDe1 noDe1 - 17 days ago 10
Javascript Question

Joining the result of two different url requests together

I have a Node js program, however I am struggling to understand how to join the response details together in one variable.

Let me explain what I mean:


  1. I have different URL's which I do a request.get on all of them at the same time (it needs to be the same time).

  2. The response I get are different, depending on the URL ending - the response can either be a
    Dog-List
    or a
    Cat-List

  3. I then need to get information from the
    Dog-list
    , therefore I have an if statement saying that if the response contains a
    Dog-List
    , print out 'in dog list' and run the required code.

  4. If it is a
    Cat-List
    , print out 'in cat list' and run the required code.

  5. FINALLY, get the dog and cat info and join it together to form one variable.



This is the code:

var urls = ['http://111.1:1111/cats', 'http://111.1:1111/dogs'];

function test() {
async.map(urls, function(url, cb) {
request.get(url, function(error, response, body) {
if (error) {
console.log(error);
} else {
parseString(body, function(result) {

var age1 = 0;
var age2 = 0;

if (result.Return['Cat-List']) {
console.log('in cat list');
age1 = result.Return['Cat-List'].age;

} else if (result.Return['Dog-List']) {
console.log('in dog list');
age2 = result.Return['Dog-List'].age;
}

data = age1 + age2;
console.log(data);
});

}
});
});
}


However, as you would expect, because I am using an 'if' statement after getting a response from each URL, I will only go either into the dog or cat list, therefore my results can never be combined.

This is what I get printed when I run the program:

in cat list
2
in dog list
3


that is because the cat age is 2, and the dog age is 5.

This is what I would want the cmd to look like:

in cat list
in dog list
5


Is there a different way to do this without using the if statements? Or just doing something else to join the results together? I am just struggling to get to this result. Any help would be appreciated!

rsp rsp
Answer

If you want the data value to be used anywhere then you need to run a callback with it with something like:

cb(null, data);

Where null means that there's no error.

You also need another callback as an argument to async.map to call when everything is done with something like:

function test() {
    async.map(urls, function (url, cb) {
        // code to run for every value
    },
    function (err, results) {
        // code to run after everything is finished
        // here you can sum the results
    });
}

For more info see:

Full example - not tested:

var urls = ['http://111.1:1111/cats', 'http://111.1:1111/dogs'];

function test() {
    async.map(urls, function (url, cb) {
        request.get(url, function(error, response, body) {
            if (error) {
                console.log(error);
            } else {
                parseString(body, function(result) {
                    var age = 0;
                    if (result.Return['Cat-List']) {
                        console.log('in cat list');
                        age = result.Return['Cat-List'].age;
                    } else if (result.Return['Dog-List']) {
                        console.log('in dog list');
                        age = result.Return['Dog-List'].age;
                    }
                    cb(null, age);    
                });

            }
        });
    }, function (err, results) {
        var sum = results.reduce((a, b) => a + b, 0);
        console.log(sum);
    });
}