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

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

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