As part of my personal project that makes use of Azure AI APIs and Node.js/Express, I'm trying to respond to a get request to a /viewText
route with text and key phrases extracted from an uploaded image/document.
Here's the code that should log that data to the console:
app.get(`/viewText`, async (req, res) => {
const answerReadResult = await readOperation(`${__dirname}\\uploads\\answer`);
const markReadResult = await readOperation(`${__dirname}\\uploads\\mark`);
Promise.all([
readOperation(`${__dirname}\\uploads\\answer`),
keyPhraseExtractor(answerReadResult),
readOperation(`${__dirname}\\uploads\\mark`),
keyPhraseExtractor(markReadResult),
]).then((data) => {
console.log("data from Promise.all: ", data);
});
});
I expected to receive two strings from the readOperation function and an array of strings from the keyPhraseExtractor, but all I get is: data from Promise.all: [ undefined, [], undefined, [] ]
Here's the code for the readOperation function(which takes an image and returns the identifiable text as an array of strings)
const readOperation = async (path) => {
fs.readdir(path, (err, files) => {
if (err) console.log(err);
files.forEach(async (file) => {
try {
const results = await getTextFromImage(
`${__dirname}\\uploads\\answer\\${file}`
);
console.log("data from readOperation: ", results.join(" "));
return results;
} catch (err) {
console.error(err);
}
});
});
};
and the log is: data from readOperation: you must be the change you want to see in the world !
N.B: this logged data is what I'm trying to return from this function (without the join("")) but I'm getting undefined as is apparent from the Promise.all
The keyPhraseExtractor function (shown below) expects an array of strings and should return the key phrases in those strings (also as an array of strings)
const keyPhraseExtractor = async (dataFromReadOperation) => {
let results = dataFromReadOperation;
try {
const data = await keyPhraseExtraction(textAnalyticsClient, results);
console.log("data inside keyPhraseExtractor: ", data);
return data;
} catch (err) {
console.error(err);
}
};
The log is data inside keyPhraseExtractor: []
readOperation
is wrong. Within theasync
function you're kicking offreadDir
and passing it a callback, but nothing in theasync
function waits for the callback value (soreadDir
is likely not done by the time it returns), and there's noreturn
inreadOperation
, so it absolutely returnsundefined
.Yes, you have
return results
, but that's within theforEach
async
arrow function. There's nothing that would pass that out to the enclosing promise thatreadOperation
returns.That said, I'd suggest using the
fs
Promises API so you're not mixing err-first callback APIs andasync
functions.