Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda