.
Hello, with the help of the following code, I try to retrieve a table of "Etablissement" via asynchronous queries. The last line is underlined in red with this message:
Type 'Promise<(string[] | undefined)[]>' is missing the following properties from type 'Establishment[]': length, pop, push, concat, and 29 more.
If anyone has an idea, any suggestions for resolution would be appreciated. Thanks in advance for your help.
Here is a code snippet :
interface GetApplicationHook {
getApplication: (id?: string) => Promise<ApplicationToSave>
defaultApplication: ApplicationToSave
getOneEtablissement: (uai?: string) => Promise<Etablissement>
toEtablissementsList: (ouList: string[]) => Etablissement[]
}
[...]
const getOneEtablissement = (uai?: string): Promise<Etablissement> => {
if (uai) {
return loadOneEtablissement(uai)
.then((etab: Etablissement) => {
return {
nom: etab.nom,
uai: etab.uai,
ou: etab.ou,
description: etab.description,
}
})
.catch(() => Promise.reject("Erreur à la récupération de l'établissement=" + uai))
}
return Promise.resolve(defaultEtablissement)
}
const fromOuToUai = (ou: string): string => {
try {
return ou.split(',')[0].split('=')[1]
} catch (error) {
return ou
}
}
const toEtablissementsList = (ouList: string[]): Etablissement[] => {
const etabList = Promise.all(
ouList.map(ou => {
try {
const uai = fromOuToUai(ou)
getOneEtablissement(uai)
.then((data: Etablissement) => data)
.catch(() => Promise.reject())
} catch (error) {
return ouList
}
})
)
return etabList
}
[...]
I have been looking for information about how Promises
work and how to extract the interesting data from them. If I understood correctly, this should be done through the .then() method along with the .catch() method… but that doesn’t seem to be enough here. The problem is about typing apparently but I don’t see where the problem lies.
4
Answers
Here the full component code, if this can help (the attribute listeOrganisations at the bottom needs to be a Etablissement[]) :
The issue with the code is that the toEtablissementsList function is returning a Promise instead of an array of Etablissement objects.
try this:
The issue with the last line of your code is that
etabList
is a Promise that resolves to an array ofEtablissement[]
, not an array of Etablissement objects. This is becausePromise.all()
returns a promise that resolves to an array of the resolved values of the input promises.You need to modify your
toEtablissementsList()
function to await the resolution of thePromise.all()
call and then return the resultingEtablissement[]
array. You can also simplify your code by removing the unnecessary try-catch blocks.Something like this:
you can create an array of
Promise<Etablissement>
objects by mapping over theouList
array and callinggetOneEtablissement(
) for eachuai
. Then usePromise.all()
to wait for all the promises to resolve, and return the resultingEtablissement[]
array.Also changed the return type of
toEtablissementsList()
toPromise<Etablissement[]>
, since the function is asynchronous and returns aPromise
.Edit for request:
To extract an Etablissement[] from a Promise<Etablissement[]>, you need to wait for the promise to resolve using the await keyword or using the then() method.
Hi and thanks for all your answers,
I finally used the function suggested by Benji after which I made some other modifications in my code. Here is the final result if it can help anyone.
Good day to you !