首页 > 解决方案 > 生成 JSON 永远不一样

问题描述

我不明白什么时候启动脚本。我的 JSON 不一样了...我尝试更改 Timeout 的时间但同样的问题

//   Call the file functions.js
var functions = require('./functions.js')

/* 
Function getAllIssueForSCII displays all the issues in the form of a JSON and that browses all the keys that are in the SCII project
Function pushInitialization initializes the computer score card to 80 on Jira

*/

functions.getAllIssueForSCII().then(function(json){
    for (let i=0; i<50;i++){      
        console.log(json.issues[i].key);


        /* 
        A delay is added so that Jira can correctly recover the value 80.
        Thanks to this value, we can do all the calculation  (Function calculate)  
        Function pushComputerScoreCard  push the final value into the computer score card.
        Function generateJSON generates a JSON.
        Function replaceCharacter solve the problem of array inside the JSON 
        */

        setTimeout(function() {

            functions.calculate(json.issues[i]);
            functions.generateJSON(json.issues[i].key);
            functions.replaceCharacter();


        }, 1000)
    }
});

函数 getAllIssueForSCII()

function getAllIssueForSCII(){
    var options = {
        method: 'GET',
        url: 'https://***.atlassian.net/rest/api/3/search?jql=project=SCII+AND+issuetype=Story+AND+resolution=Unresolved+AND+status%20in("In%20Review","In%20Inspection","Inspected","Inspection%20Needed","Feedback")',
        headers: {  
            'Accept': 'application/json',
            'Authorization': "Basic ***"
        }
    };


    return new Promise(function (resolve) {
        request(options, function (error, response, data) {
            if (!error && response.statusCode === 200) {
                console.log('JIRA login success!!')
                var json = JSON.parse(data);
                console.log("Filtered Issues: ", { issues: json.issues.filter(issue => issue.expand.key) });
                resolve(json);            
            } else {
            }

        })
    })
}

函数 calculate() 它只是一些计算

函数 generateJson()

function generateJSON(key){
    var table = []


    table.push({"executionDate":dateTime,
    "issueID":key,
    "priority":{
        "jira": priority, 
        "computed":score1
    },
    "expectedValue":{
        "jira": expected, 
        "computed":score2
    },
    "customerCr":{  
        "jira":customer,
        "computed":score3
    },
    "effortEstimate":{  
        "jira":effort_estimate,
        "computed":score4
    },
    "pmWeight":{  
        "jira":pm_weight,
        "computed":score5
    },
    "netRevenue":{  
        "jira":net_revenue,
        "computed":score6
    },
    "scoreCard":{  
        "original":score_card,
        "computed":total
    }
})

var json = JSON.stringify(table,null, 2);

fs.appendFile('templateLog.json', json, 'utf8', function (err) {
    if (err) console.error(err)

});

}

函数replaceCharacter(用,替换][)

function replaceCharacter(){
    const options = {
        files: 'templateLog.json',
        from: /]\[/g,
            to: ',',
        };

        replace(options, (error, results) => {
            if (error) {
                return console.error('Error occurred:', error);
            }
            console.log('Replacement results:', results);
        });
    }

实际结果:

预期结果:

谢谢你的帮助

标签: javascript

解决方案


推荐阅读