fs.writeFileSync() not able to write all the files in Nodejs?

Viewed 29

I am trying to create a txt file in Nodejs and insert data into it, but it is not able to create all the files and put input into that file.

for example - I have 6 results from database which I need to put in a separated 6 .txt file, but sometimes it will creates 4 files and write 4 data into it and sometimes create 5 files or 3 and put data into that file accordingly. but **it is unable to create 6 files all the time. **

In .txt files I am writing texts i.e. - translation

sample data -

Swami Vivekananda (1863-1902) what the face .look in the mirror Every beat of our heart makes us selfish orders to be. but on the other hand, Ank Parashakti says that the only Selflessness is the only means of good. run without direction man's destiny From the elements of life simple daily macro tasks on review till

ImagesNames.forEach((element) => {
detecText(element)
  .then(async (result) => {

    texts = result; 
    let imagesdir = element.replace("../nonenglishinput/", "");

    finaltextsave = imagesdir.replace(".jpg", ".txt"); 
    
    let translations = await translateText(result);

    translations.forEach((translation, index) => {
      let [path] = finaltextsave.split(".");

      // dir is directory name crating .txt and adding resulted text in txt
      fs.writeFileSync([dir + "/" + path, ".txt"].join(""), translation);


    });
})
.catch((err) => console.log (err + + element));

});

In above code it is trying to create .txt file and put data into that file according to API request data, file in the data is correct and accurate but sometimes this missed creating file and putting data, I am clueless here.

See I have 6 files which is writing into .txt enter image description here

but it only writes 3 or 4 or 2 sometimes not precisely 6, fs.writeFileSync() should write equally to input files pass, if 6 pass then 6 files needs to create!

enter image description here

0 Answers
Related