Currently, I am utilizing Node JS along with the replace-in-file library for my project.
Within a specific file named functions.js, I have implemented various functions.
Furthermore, in another file named index.js, I have added code to call these functions.
Initially when I run the script, everything works smoothly. However, upon relaunching the script, the replacement of '][' with ',' does not occur as expected.
In Function.js:
var priority = "";
var expectedValue = "";
var score1 = "";
var score2 = "";
/* some function*/
function generateJSON(key){
var table = []
// JSON object structure
table.push({
"executionDate": date,
"issueID": key,
"priority":{
"jira": priority,
"computed": score1
},
"expectedValue":{
"jira": expected,
"computed": score2
}
})
var json = JSON.stringify(table,null, 2);
fs.appendFile('templateLog1.json', json, 'utf8', function (err) {
if (err) console.error(err);
});
}
function replaceCaracter(){
const options = {
files: 'templateLog1.json',
from: '][',
to: ',',
};
replace(options, (error, results) => {
if (error) {
return console.error('Error occurred:', error);
}
console.log('Replacement results:', results);
});
}
In Index.js:
setTimeout(function() {
functions.getAllIssueForSCII().then(function(json){
for (let i=0; i < json.issues.length; i++){
functions.generateJSON(json.issues[i].key);
functions.replaceCaracter();
}
});
}, 1000)
Actual Result :
[
{
"executionDate": 1556197884153,
"issueID": "SCII-10",
"priority": {
"jira": "Lowest",
"computed": -25
},
"expectedValue": {
"jira": "Low",
"computed": -10
}
}
,
{
"executionDate": 1556197896877,
"issueID": "SCII-7",
"priority": {
"jira": "Low",
"computed": -10
},
"expectedValue": {
"jira": "Low",
"computed": -10
}
}
]
Expected Result:
[
{
"executionDate": 1556197884153,
"issueID": "SCII-10",
"priority": {
"jira": "Lowest",
"computed": -25
},
"expectedValue": {
"jira": "Low",
"computed": -10
}
}
,
{
"executionDate": 1556197884153,
"issueID": "SCII-7",
"priority": {
"jira": "Low",
"computed": -10
},
"expectedValue": {
"jira": "Low",
"computed": -10
}
}
,
{
"executionDate": 1556197896877,
"issueID": "SCII-10",
"priority": {
"jira": "Lowest",
"computed": -25
},
"expectedValue": {
"jira": "Low",
"computed": -10
}
}
,
{
"executionDate": 1556197896877,
"issueID": "SCII-7",
"priority": {
"jira": "Low",
"computed": -10
},
"expectedValue": {
"jira": "Low",
"computed": -10
}
}
]