1

I'm writing a node app for generating sitemaps and am having trouble with a particular part that I need to (i) Loop through a JSON page containing a number of products with specific URL keys and check if the URL exists for each of these products and (ii) if the URL does exist it needs to print this URL to the console.

var request = require("request");

var url = "http://linktoJSON"

//reads the JSON from the provided URL
request({
url: url,
json: true
}, function (error, response, body) {

    //loops through each product on the page
    for (i = 0; i < body.length; i++) { 
        //checks if the URL exists for each product
        request('http://www.example.com/product/' + (body[i].urlKey), function (err, response) {
            //if the product page does exist
            if (response.statusCode === 200) {
                    //print the product URL
                    console.log (('<xtml:link\nrel="alternate"\nhreflang="x-default"\nhref="http://www.example.com/' + body[i].urlKey) +'"\n/>');
                    }
            //if the product doesn't exist it does nothing
            else
                {}
    })}

});

This works fine up to the point where it should print the product URL, at this point it is not recognising [i] as a number and is giving me an error. Is there any other way to pass the value of [i] to the console.log or get it to print the exact same link as it is using for the request?

denisq91
  • 155
  • 1
  • 10

2 Answers2

1

Try:

var request = require("request");

var url = "http://linktoJSON"

//reads the JSON from the provided URL
request({
    url: url,
    json: true
}, function (error, response, body) {
    //loops through each product on the page
    for (i = 0; i < body.length; i++) { 
        //checks if the URL exists for each product
        processKey(body[i].urlKey);
    } 
});

function processKey(key) {
    request('http://www.example.com/product/' + key, function (err, response) {
        //if the product page does exist
        if (response.statusCode === 200) {
            //print the product URL
            console.log('<xtml:link\nrel="alternate"\nhreflang="x-default"\nhref="http://www.example.com/' + key +'"\n/>');
        }
        //if the product doesn't exist it does nothing
        else
        {
        }
    });
}

And see this question for information: JavaScript closure inside loops – simple practical example

Community
  • 1
  • 1
smremde
  • 1,800
  • 1
  • 13
  • 25
  • 1
    Thank you, it is now returning a value. However, I am having difficulty returning any value rather than the last element in the array even after looking at the examples online. – denisq91 Jan 21 '15 at 11:54
0

Alternatively, if you are using an interpreter that supports it, you can use the let keyword in your for statement.

var request = require("request");

var url = "http://linktoJSON"

//reads the JSON from the provided URL
request({
url: url,
json: true
}, function (error, response, body) {

    //loops through each product on the page
    for (let i = 0; i < body.length; i++) { 
        //checks if the URL exists for each product
        request('http://www.example.com/product/' + (body[i].urlKey), function (err, response) {
            //if the product page does exist
            if (response.statusCode === 200) {
                    //print the product URL
                    console.log (('<xtml:link\nrel="alternate"\nhreflang="x-default"\nhref="http://www.example.com/' + body[i].urlKey) +'"\n/>');
                    }
            //if the product doesn't exist it does nothing
            else
                {}
    })}

});
smremde
  • 1,800
  • 1
  • 13
  • 25