Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
Is there any way to stop overwriting the json file?

actually this code works as expected but, if I do input a different userId, it still do overwrite the existing user (not the file) and the ideia was to add another one

 const user = userId;
 const userObj = {[user]:valueX};
    words.users = userObj;
    fs.writeFileSync('words.json', JSON.stringify(words,null,2), finished);

input comes from here (readline-sync)

let userId = input.question('Enter yourUserId: ');

and yes i'm reading the file first

let words = JSON.parse(fs.readFileSync('words.json'));
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

const userObj = {[user]:valueX};
words.users = userObj;

The object you assign to words.users needs to include all the existing users and not just your new one.

const userObj = {...words.users, [user]:valueX };
about 4 years ago · Santiago Trujillo Report

0

From your code it looks like words.users is an object and in that case

if(!words.users) words.users = {};  // Make sure the words users are created
words.users[user] = valueX;

This would give you an words.users object that look like this

{user1: valueX1, user2: valueX2}

And you could access like this

console.log(words.users[user1]); // valueX1
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!