0

Hey i want to append data to my json file but its Acutally updating the old data please help me refine the code

'use strict';
const fs = require('fs');

function handleSubmit(event) {
    event.preventDefault();

    const data = new FormData(event.target);

    const value = Object.fromEntries(data.entries());
  
    fs.writeFileSync('file.json', JSON.stringify([value], null, 2));
}

const form = document.querySelector("form");
form.addEventListener("submit", handleSubmit);

file.json

{
  "server_name": "ooo1",
  "server_ver": "office",
  "server_type": "false"
}

what i want that

[
{
  "server_name": "ooo1",
  "server_ver": "office",
  "server_type": "false"
},
{
  "server_name": "ooo2",
  "server_ver": "office",
  "server_type": "false"
}
]

i want the data to append not update please help me with best possible ways to help me make my new open source project

HEALER
  • 31
  • 2

1 Answers1

0

I think you need append flag

fs.writeFileSync('file.json', JSON.stringify([value], null, 2), {flag:'a+'});

Like this

FireAnt121
  • 21
  • 5