I'm having trouble making calls to API endpoints from an array. The array contains values that I need to feed into the endpoint in order to get data that I need. The trouble I am having is when I console log the data from the api call I receive JSON text. However, when I try to parse the text and get a specific value from the JSON text it is not giving me the results. Here is the code I am working on:
var http = require("http");
var areaCodes = ["'0071950')", "'0072850')", "'0073450')", "'0075700')", "'0076450')", "'0078700')", "'0900001')", "'0900000')"];
var results = [];
var encode = [];
//source: http://stackoverflow.com/questions/17811827/get-a-json-via-http-request-in-nodej
for(var i = 0; i < areaCodes.length; i++){
var realmStatus = "http://api.dol.gov/V1/Statistics/OES/OE_SERIES/?KEY=XXXXXXXXXXXXXXXXXXXXXXXXXX&$filter=(OCCUPATION_CODE eq '151131' ) and (AREA_CODE eq " + areaCodes[i];
encode.push(encodeURI(realmStatus));
var options = {
host: 'api.dol.gov',
path: encode[i],
type: 'GET',
dataType: 'json',
headers: {'accept' : 'application/json'}
};
console.log("Start");
var x = http.request(options,function(res){
console.log("Connected");
var str = '';
res.on('data', function(chunk) {
str += chunk;
});
res.on('data',function(data){
if(res.statusCode == 200){
//console.log("res status is good");
try{
var dataA = JSON.parse(str);
//run a for loop
for(var h = 0; h < dataA.d.results.length; h++){
var seriesNum = dataA.d.results[h].SERIES_ID;
array.push(seriesNum);
console.log(seriesNum);
}
//end for
}catch(e){
console.log('Error parsing JSON');
}
}
});
});
x.end();
}//end for loop
I'd appreciate if someone would be willing to explain if there is anything I did incorrectly, or if there is something I am missing. Thanks!