循环javascript回调函数的内部函数仅运行一次

时间:2019-07-02 19:33:24

标签: javascript rest rally

我正在尝试将Rally的REST API与Jav​​ascript一起使用来从JSON文件读取缺陷数据,使用每个JSON索引的“ url”属性来解析来自Rally的讨论数据并将其全部写入文件中。

这带来了一些异步问题,经过大量尝试后我无法解决。我很亲近,但仍然有一个问题。

这是我的代码:

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(/<[^>]+>|&nbsp;/g, "");
        description = dataFile[i].description.replace(/<[^>]+>|&nbsp;/g, "");
        resolution = dataFile[i].resolution.replace(/<[^>]+>|&nbsp;/g, "");
        resolutionSummary = dataFile[i].resolutionSummary.replace(/<[^>]+>|&nbsp;/g, "");
        title = dataFile[i].title.replace(/<[^>]+>|&nbsp;/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);
        }

    )



});

该代码为JSON文件中的每个索引(其中的1142个)正确循环,并且对于每个对象,它使用回调启动“ getDiscussion”功能。此函数应使用URL,并使用rest api请求拉讨论。问题在于,此请求是与回调分开运行的,这意味着一次又一次地仅调用最后一个回调URL。

    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

我只希望REST API请求采用循环中的当前URL,如下所示:

getDefects(function getDiscussion(){
    console.log(url);
    process.env.NODS_TLS_REJECT_UNAUTHORIZED = "0";

,并在当前循环的请求中使用相同的URL。第二个控制台日志应打印完全相同的URL

  function(error,response,data){
            //console.log(data);
            console.log("flag"+url);
        }

0 个答案:

没有答案