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()
inTranslateText()
and response of translate text I am trying to save in .txt file where filename
will be ImagesNames array or detecTxt parameterNow I am passing this array filename in
writeFileSync
where text is coming fromtranslateText() 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.
Updated as suggested by below answers -
still getting error
but a bit different.
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 array.
ImagesNames.forEach((element) => {
detecText(element)
.then(async (result) => {
texts = result;
let imagesdir = element.replace('public/', '');
finaltextsave = imagesdir.replace('.jpg', '.txt');
fs.writeFileSync(finaltextsave, await translateText(result));
})
.catch((err) => console.log(err + element));
});