I am attempting to use Rally's REST API with Javascript to read defect data from a JSON file, use the "url" property of each JSON index to parse discussion data from Rally and write it all to a file together.
This brings up some Async issues which I have been unable to solve after much attempting. I am very close but still have one problem.
Here is my code:
const request = require("request");
const config = require("./config.js");
var fs = require("fs");
let dataFile = require('./Defect2');
var defectTable={};
defectTable.table=[];
var keys = Object.keys(dataFile).length;
var formattedId,description,resolution,resolutionSummary,title,discussion;
var url;
var number;
var flag;
function getDefects(callback){
console.log("Getting Defects");
for(let i = 0; i < keys; i++){
console.log(i);
formattedId = dataFile[i].formattedId.replace(/<[^>]+>| /g, "");
description = dataFile[i].description.replace(/<[^>]+>| /g, "");
resolution = dataFile[i].resolution.replace(/<[^>]+>| /g, "");
resolutionSummary = dataFile[i].resolutionSummary.replace(/<[^>]+>| /g, "");
title = dataFile[i].title.replace(/<[^>]+>| /g, "");
discussion = dataFile[i].discussion;
var obj = {formattedId:formattedId,description:description,resolution:resolution,
resolutionSummary:resolutionSummary,title:title,discussion:discussion};
url = discussion;
defectTable.table.push(obj);
number=i;
callback();
}
}
getDefects(function getDiscussion(){
console.log(url);
process.env.NODS_TLS_REJECT_UNAUTHORIZED = "0";
request(
{
uri: url,
method: "GET",
proxy: "http://" + config._USERNAME + ":" + config.APASSWORD + "@proxy.blah.com:8080",
headers: {
zsessionid: config.API_KEY
}
},
function(error,response,data){
//console.log(data);
console.log("flag"+url);
}
)
});
The code correctly loops for each index in the JSON file (1142 of them) and for each object it uses the callback to start the "getDiscussion" function. This function should take the URL and use the rest api request to pull the discussion. The problem is that this request is being run separate from the callbacks mean that only the last callback URL is being called, over and over again.
https://rally1.rallydev.com/slm/webservice/v2.0/Defect/315669034432/Discussion
1138
https://rally1.rallydev.com/slm/webservice/v2.0/Defect/315696460760/Discussion
1139
https://rally1.rallydev.com/slm/webservice/v2.0/Defect/315877454860/Discussion
1140
https://rally1.rallydev.com/slm/webservice/v2.0/Defect/316280129492/Discussion
1141
https://rally1.rallydev.com/slm/webservice/v2.0/Defect/316554593564/Discussion
1142
https://rally1.rallydev.com/slm/webservice/v2.0/Defect/316634777100/Discussion
flaghttps://rally1.rallydev.com/slm/webservice/v2.0/Defect/316634777100/Discussion
flaghttps://rally1.rallydev.com/slm/webservice/v2.0/Defect/316634777100/Discussion
flaghttps://rally1.rallydev.com/slm/webservice/v2.0/Defect/316634777100/Discussion
flaghttps://rally1.rallydev.com/slm/webservice/v2.0/Defect/316634777100/Discussion
flaghttps://rally1.rallydev.com/slm/webservice/v2.0/Defect/316634777100/Discussion
flaghttps://rally1.rallydev.com/slm/webservice/v2.0/Defect/316634777100/Discussion
flaghttps://rally1.rallydev.com/slm/webservice/v2.0/Defect/316634777100/Discussion
I simply want the REST API request to take the current URL in the loop as printed here:
getDefects(function getDiscussion(){
console.log(url);
process.env.NODS_TLS_REJECT_UNAUTHORIZED = "0";
and use that same URL in the request for the current loop. The second console log should print the exact same URL
function(error,response,data){
//console.log(data);
console.log("flag"+url);
}