Reputation:
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.
Upvotes: 1
Views: 201
Reputation: 837
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.
Upvotes: 3