Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda