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

122
Views
forEach method skips objects while iterating

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.

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

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.

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!