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

364
Views
Firebase snapshot.forEach(async () => {}) not working properly

I have a code here, that loads posts from firebase collection "posts" But it does not work the way I expect. There are two documents in collection "posts" But the result of console.log() is this. It initially has 0 items, and only after it is populated with two documents. I think that the problem is in this line:

const photoUrl = await getDownloadURL(ref(storage, data.photoPath))

Because when I remove it, it works fine(array is populated before it is logged in the console, like this). Please help!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Probably you need to consider reconstruct your code a little bit. The problem is that the code ends before the asynchronous operations finish. Even though you added await when you are trying to download, but the entire operation in the foreach bracket async (doc) => {....} is still an asynchronous operation.

What you can do is to reorganize your code into this way:

const promiseList = []

snapShot.forEach(doc => promiseList.push(asyncFunctionToCompleteYourData(doc)))

await Promise.all(promiseList)

Besides, you probably needs to check your lint rules, because usually for JavaScript it should directly throw an error if you are directly writing async code in a for loop.

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!