I would like below the "queryPexels" function to return the value for the imgSuggestions variable, which does not happen (EDITTED) I pasted the rest of the code, I believe the problem is somewhere in the syntax of the promise all
let foundProduct; //product id
let imgSuggestions; //pexels
let labelSeggestions; // OpenAI
let headingSuggestions; // OpenAI Title
let fontDownloadLink; // Font link
if (
foundRegistries.products.some((product) => product.id === productId)
) {
foundProduct = foundRegistries.products.filter(
(product) => product.id === productId
);
let queryImage = foundProduct[0].productName.toString()
return new Promise(async (resolve, reject) => {
try {
await Promise.all([
(imgSuggestions = await queryPexels(
pexelsConfig,
queryImage,
photosPerPage,
)),
(labelSeggestions = await queryOpenAiLabel(
openAiConfig,
foundProduct[0].productName,
foundProduct[0].targetAudience
)),
(headingSuggestions = await queryOpenAiHeading(
openAiConfig,
foundProduct[0].productName
)),
(fontDownloadLink = await getFonts(
googleConfig,
foundRegistries.font,
foundRegistries.fontVariant
)),
]).then(() =>
resolve(
{
productId : productId,
imgSuggestions : imgSuggestions,
labelSeggestions : labelSeggestions,
headingSuggestions : headingSuggestions,
fontDownloadLink : fontDownloadLink,
}
)
);
} catch (error) {
throw error;
}
});
} else {
return {
Error: "No product found. Please check provided product ID.",
};
}
It's her my function async queryPexels:
async function queryPexels(pexelsConfig, query, photosPerPage) {
// const client = createClient(pexelsConfig.key);
queryOpenAiKeywords(
invocation.openAiConfig,
query
).then(res => {
const query = res
translateTxt(query).then(translate =>{
const trad = translate.toString()
const client = new Client({ apiKey: pexelsConfig.key});
getPexelsPhotos(trad,client,photosPerPage).then(response =>{
const photos = choosePhotos(response.photos)
console.log(photos) // THIS PHOTOS IS PRINTED BUT IMGSUGGESTIONS IS EQUAL TO UNDEFINED
return photos // BECAUSE THIS RETURNS ISN'T GET OF BY PROMISE ALL
}).catch( err => {
console.log(err)
})
}).catch( err => {
console.log(err)
});
})
}
When I print the imgSuggestions after Promise.all i've got undefined value, but the queryPexels console.log(photos) is succeeding.
CodePudding user response:
Added missing returns, should fix the issue
async function queryPexels(pexelsConfig, query, photosPerPage) {
// here
return queryOpenAiKeywords(
invocation.openAiConfig,
query
).then(res => {
const query = res
// here
return translateTxt(query).then(translate =>{
const trad = translate.toString()
const client = new Client({ apiKey: pexelsConfig.key});
// here
return getPexelsPhotos(trad,client,photosPerPage).then(response =>{
const photos = choosePhotos(response.photos)
console.log(photos)
return photos
}).catch( err => {
console.log(err)
})
}).catch( err => {
console.log(err)
});
})
}