Home > Software design >  WriteFileSync is not working while saving response in .txt file in Nodejs?
WriteFileSync is not working while saving response in .txt file in Nodejs?

Time:08-27

I have a async function translateText() which return some response, I am trying to put that response in a different file where the function will call, but it is not working at all. giving error.

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. 
Received an instance of Promise
  • I have a array ImagesNames which contains file name.

  • detecText() is coverting txt from Images and pass response in translateText()

  • After this I am trying to use the response of detecText() in TranslateText() and response of translate text I am trying to save in .txt file where filename will be ImagesNames array or detecTxt parameter

  • Now I am passing this array filename in writeFileSync where text is coming from translateText() function

translateText function

    const target = "en";
    
    const translateText = async function translateText(text) {
      let [translations] = await translate.translate(text, target);
      translations = Array.isArray(translations) ? translations : [translations];
    
      translations.forEach((translation, i) => { 
        return translation;
      });
    }; 

Using function to write response in .txt

   ImagesNames.forEach((element) => {
    detecText(element)
    .then((result) => {
    texts = result;
    let imagesdir = element.replace("public/", "");
    finaltextsave = imagesdir.replace(".jpg", ".txt");
     fs.writeFileSync(finaltextsave, translateText(result));

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

    });

How Can I save this response of translateText() and parameter of detecText() as a file name for every loop running or every parameter.

CodePudding user response:

Every async function automatically returns a Promise. Therefore the return values of translateText is a Promise. fs.writeFileSync on the other hand can only work with strings or buffers. To resolve the issue you are facing the callback of your .then call on detecText also has to be asynchronous so you can await translateText.

ImagesNames.forEach((element) => {
  detecText(element)
    .then(async (result) => {
      //  ^^^^^ add async here to create async context
      texts = result;
      let imagesdir = element.replace('public/', '');
      finaltextsave = imagesdir.replace('.jpg', '.txt');
      fs.writeFileSync(finaltextsave, await translateText(result));
      //                              ^^^^^ add await here
    })
    .catch((err) => console.log(err   element));
});

CodePudding user response:

You could try this:

ImagesNames.forEach(async (element) => {
    try {
      let text = await detecText(element);
      let translatedText = await translateText(text);
      let imagesdir = element.replace("public/", "");
      finaltextsave = imagesdir.replace(".jpg", ".txt");
      fs.writeFileSync(finaltextsave, translatedTexts);
    } catch (err) {
      console.log(err   element)
    }
  }
);
  • Related