Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!