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

291
Vistas
Failed to append data in .json file using node

I want to append my data which is array of object format to the existing .json file so I have written code for it, but on stackoverflow I noticed so many developers suggesting before appending to existing json file first read file and then push new data to existing json file. So I followed this and made changes according to this but getting error :

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined

Code written in node

 newdata=[
  {
    UserId: '11',
    UserName: 'harry',
   
  },
  {
    UserId: 12,
    UserName: 'David',
  }
];

     fs.readFile('results.json', function (err, data) {
    if(data === '') {
        json = JSON.parse(newdata);
        json.push(newdata);
    }
    fs.writeFile("results.json", JSON.stringify(json));
  })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This error is because you are using the .writeFile in a wrong way.

Try something like this:

fs.writeFile('results.json', JSON.stringify(newData), function(err) {
    if (err) throw err; 
});

A short explanation:

The ERR_INVALID_CALLBACK in the error message is the missing of the function informed in the last parameter in the example above.

fs.writeFile(<file>, <content>, <callback>)
about 4 years ago · Juan Pablo Isaza 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