Arrays 在将URL记录到阵列中的控制台之前,请检查URL是否存在
我正在编写一个用于生成站点地图的节点应用程序,但有一个特定部分遇到了问题,我需要(I)循环浏览包含许多具有特定URL键的产品的JSON页面,并检查这些产品是否存在URL,以及(ii)如果URL确实存在,则需要将此URL打印到控制台Arrays 在将URL记录到阵列中的控制台之前,请检查URL是否存在,arrays,json,node.js,request,Arrays,Json,Node.js,Request,我正在编写一个用于生成站点地图的节点应用程序,但有一个特定部分遇到了问题,我需要(I)循环浏览包含许多具有特定URL键的产品的JSON页面,并检查这些产品是否存在URL,以及(ii)如果URL确实存在,则需要将此URL打印到控制台 var request = require("request"); var url = "http://linktoJSON" //reads the JSON from the provided URL request({ url: url, json: tru
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
{}
})}
});
var请求=要求(“请求”);
变量url=”http://linktoJSON"
//从提供的URL读取JSON
请求({
url:url,
json:true
},函数(错误、响应、正文){
//循环浏览页面上的每个产品
对于(i=0;i
在打印产品URL之前,这一切都很正常,此时它没有将[i]识别为一个数字,并且给了我一个错误。是否有其他方法将[i]的值传递到console.log或让它打印与请求使用的完全相同的链接?尝试:
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
{
}
});
}
var请求=要求(“请求”);
变量url=”http://linktoJSON"
//从提供的URL读取JSON
请求({
url:url,
json:true
},函数(错误、响应、正文){
//循环浏览页面上的每个产品
对于(i=0;i
有关信息,请参见此问题:
或者,如果您使用的是支持它的解释器,则可以在for语句中使用
let
关键字
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
{}
})}
});
var请求=要求(“请求”);
变量url=”http://linktoJSON"
//从提供的URL读取JSON
请求({
url:url,
json:true
},函数(错误、响应、正文){
//循环浏览页面上的每个产品
对于(设i=0;i
谢谢,它现在正在返回一个值。但是,即使在线查看了示例,我也很难返回任何值,而不是数组中的最后一个元素。