I have an issue that only occurs on the Glitch server, but not on my local machine. My script gets data from an API > processes it and ultimately pushes it to a JSON file.
The problem is that it pushes each object in an array twice to the JSON file. Not only does this only occur on my Glitch server, but it also works just fine on the first index of my JSON array. He starts doing to duplicate it after the first array index.
I can imagine my code could be faulty (as I’m very inexperienced), but then again: it does not happen on my local machine.
Code:
async function updateCooldown(temp_cooldown_list, path) {
var data = await fs.readFileSync(path)
var arrayOfObjects = JSON.parse(data);
for (var j = 0; j < temp_cooldown_list.length; j++) {
for (var i = 0; i < temp_cooldown_list[j].length; i++) {
await arrayOfObjects.list[j].push(temp_cooldown_list[j][i]);
console.log(arrayOfObjects);
// this array will be pushed to the JSON file, at this moment it's still valid.
}
}
await fs.writeFile(path, JSON.stringify(arrayOfObjects), 'utf-8', function(err) {
if (err) throw err
console.log(arrayOfObjects); // at this moment all items in the array are duplicated.
// the first index of the array is pushed normally, but the other ones are duplicated...
})
}
}
// Expected output;
{
"list":[
[{"name":"Test1","time":1553589097005}],
[{"name":"Test2","time":1553589097005}],
[{"name":"Test3","time":1553589097005}]
]
}
// Real output:
{
"list":[
[{"name":"Test1","time":1553589097005}],
[{"name":"Test2","time":1553589097005}, {"name":"Test2","time":1553589097005}],
[{"name":"Test3","time":1553589097005}, {"name":"Test3","time":1553589097005}]
]
}