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

288
Views
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 answers
Answer question

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 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!