I use request to read a website then parseString to convert and later save as json. The problem is saving. Looks like "for loop" keeps looping and when is saving the information uses the last info from var cta to assign to information. I have try asyn await but it didnt work. thanks in advance.
var fs = require('fs');
var request = require('request');
var parseString = require('xml2js').parseString;
var json = {} ;
var cta = [
{
"_id": 1,
"name": "PRWeb",
"feedtype": "Misc",
"feedaddress": "https://www.prweb.com/rss2/daily.xml",
"status": true
},
{
"_id": 2,
"name": "Business Wire",
"feedtype": "Industrie",
"feedaddress": "https://feed.businesswire.com/rss/home/?rss=G1QFDERJXkJeGVtRVQ==",
"status": false
},
{
"_id": 3,
"name": "News Wire",
"feedtype": "Daily News",
"feedaddress": "https://www.newswire.com/newsroom/rss/custom/all-press-releases",
"status": true
}
];
for (var i = 0; i < cta.length; i++) {
function getDatos(url, callback) {
request(url, function(error, response, data){
callback(error, response, data);
});
}
if (cta[i].status === true) {
console.log("cta: " + cta[i].feedaddress);
agrabar = cta[i];
console.log("agrabar: " + agrabar.feedaddress);
getDatos(agrabar.feedaddress, function(error, response, data){
parseString(data, {explicitArray: false}, function (err, result) {
if (err) {
return console.log('unable to parse XML');
}
json = {rssfeeder: agrabar, feed: result.rss.channel.item};
console.log(json);
fs.appendFile ("output.json", JSON.stringify(json, null, 4), function(err) {
if (err) throw err;
console.log('complete');
});
});
});
}
}
console.log('DONE!!!')