1

I'm writing a function which takes an array of the objects containing some info, and writes that information in the JSON file. For example:

    let arr = [
      {
        creator: 'Girchi',
        title: ' Hello World',
        link: 'https://www.facebook.com/GirchiParty/posts/4693474864037269',
        pubDate: 'Sat, 07 Aug 2021 12:07:42 GMT',
        guid: '9386a170c1f52c7f388a7e1e236957e5',
        isoDate: '2021-08-07T12:07:42.000Z'
     },

     {
        creator: 'Someone else',
        title: ' Hi World',
        link: 'https://www.facebook.com/GirchiParty/posts/4693474864037269',
        pubDate: 'Sat, 07 Aug 2021 12:07:42 GMT',
        guid: '9386a170c1f52c7f388a7e1e236957e5',
        isoDate: '2021-08-07T12:07:42.000Z'
     }
   ]

and so on.. there's 16 of them.

Function that should handle that process:

function writeTheData(obj) {
    fs.readFile('./assets/data/fb.json', (err, data) => {
        if (err) throw err;
        let newsData = JSON.parse(data);
        newsData[obj.link] = {
            ...newsData[obj.link],
            link: obj.link,
            title: obj.title,
            text: obj.contentSnippet,
            articleDate: obj.pubDate,
            content: obj.content
        };
    
        newsData = JSON.stringify(newsData)
        fs.writeFileSync("./assets/data/fb.json", newsData, (error) => {
          if (error) console.log(error)
          console.log('success');
     })
   })
}

array.forEach(obj => writeTheData(obj));

It writes the data to the JSON file, but only the last object is written. For example, if there's 10 object in array, only 10th object will be written. I don't understand what's the problem here. How can I fix this?

I thought that maybe it overwrites the data and that's why I'm seeing only the last object, but in this part newsData[obj.link] I'm giving all of them unique ID's.

I'm using bunch of modules, requests etc and that's why I'm not able to show all my code. Also, there are some more properties that objects have, but they aren't in English so I didn't include them.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

3

The function fs.writeFileSync rewrites entire file, so it makes sans, by doing forEach, to have just last entry from the list.

You might want to use fs.appendFile to add to the file content, or, as a variant to accumulate result and write it once.

Alex Shtromberg
  • 740
  • 8
  • 21
  • Thank you! it worked. But now there's another problem. When objects are written, since objects in JSON needs to be separated with comma, it doesn't do it, it just appends objects so that's another error –  Aug 09 '21 at 14:23
  • 2
    You should read the file once and write it once – Raz Luvaton Aug 09 '21 at 14:41
  • @RazLuvaton you are looking for [something like this](https://stackoverflow.com/questions/36093042/how-do-i-add-to-an-existing-json-file-in-node-js) – Alex Shtromberg Aug 09 '21 at 15:03