Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
Why my variable is not been set after a await promisse
const loadData = async () => {
        try {
            const data = await GetAllForms();
            setAllForms([...data]);
        } catch (err) {
            console.log(err);
        }
    }

My variable data is receiving the data from GetAllForms (that is a promisse function), but when I try to assign to another variable is empty, In this console log for example is showing a empty array. But if i but a console.log(data) the data appears correctly, the problem is only with the dataAux I dont have a clue what is going on here. Thanks!

Here the GetAllForms

export async function GetAllForms() {
    const formRef = await dbForms.collection("forms");
    const snapshot = await formRef.get();
    const dataFormattedArray = [] as IFormsFormatted[];

    snapshot.forEach(async doc => {
        const docData = doc.data() as IForms
        const aux = [] as FormsData[];

        for (const id of docData.formsFieldId) {
            const test = await GetOneFormsField(id);
            aux.push(test);
        }

        const dataFormatted = { ...docData, formsFieldId: [...aux] }

        dataFormattedArray.push(dataFormatted);
    })
    
    return dataFormattedArray;
}

If i check de allForms variable after the setAllForms, is empty.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I solved the problem... Was the forEach indeed, i only rewrite the data fetch, replacing the forEach with a for ... of:

await Promise.all(snapshot.docs.map(async doc => {  //Fetching in Paralel the data
        const docData = doc.data() as IForms
        const aux = [] as FormsData[];
        
        for (const id of docData.formsFieldId) {
            const test = await GetOneFormsField(id);
            aux.push(test);
        }

        const dataFormatted = { ...docData, formsFieldId: [...aux] }

        dataFormattedArray.push(dataFormatted);

    }))

thanks to everyone who reply to this! Cheers!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda