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

150
Views
Firebase 9 array from onSnapshot

So I need to convert data from Firebase 9 snapshot to an array.
I've came up with something like this, but it doesn't return anything.
Check comments in my code.
I suppose, I should use promises, but can't figure out the right way.

const fetchNewMessages = async (threadId: string, latestTimestamp: any) => {
  const q = query(threadRef(threadId), orderBy('timestamp', 'asc'), startAfter(latestTimestamp));
  const messages: IMessage[] = [];
  onSnapshot(q, (querySnapshot) => {
    const docs = querySnapshot.docChanges();
    docs.forEach((change) => {
      if (change.type === "added")
        messages.push(fetchDoc(change.doc));
    });
    console.log(messages.length); // here I get 34
  });
  console.log(messages.length); // here I get 0
  return messages.reverse();
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

try to wait for "onSnapshot"

  const fetchNewMessages = (threadId: string, latestTimestamp: any) => {
  const q = query(threadRef(threadId), orderBy('timestamp', 'asc'), startAfter(latestTimestamp));
  const messages: IMessage[] = [];
  return new Promise((resolve) => {
    onSnapshot(q, (querySnapshot) => {
      const docs = querySnapshot.docChanges();
      docs.forEach((change) => {
        if (change.type === "added")
          messages.push(fetchDoc(change.doc));
      });
      resolve(messages.reverse());
    });
  });
};
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!