Node.js push() pushes twice on Glitch server, local machine works fine

#1

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}]
  ]
}
#2

Hi @coinlord, welcome to the Glitch forum!

I’m sorry for the bother! I don’t see anything on the surface that might cause this behavior, but I’d like to take a closer look in a Remix of your project; can you provide your project name?

#3

Sure no problem.

Function is called at line 57.
Function is located at line 417.

#4

Hey @coinlord I’m not seeing this behavior, and when I look at the json file in the parent project (which I gather is the project you’re actually working with) I also don’t see what I understood you to be saying was happening.

Perhaps I’m misunderstanding something - can you tell me how your json file ought to look?