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

322
Views
How to use onSnapshot in firebase? Can't work in my apps

I have made my own web apps(using vue.js) and using firestore as backend. In my firebase method, I tried to use onSnapshot method in firestore, but result was undefined.

firebase version is latest(v9).

so my code is below.

let user;
    const snapshot = await onSnapshot(collection(db, "user"), (snap: any) => {
      let user: any = [];
      snap.docs.forEach((doc: any) => {
        user.push({ ...doc.data() });
      });
      return user;
    });
    snapshot();
    console.log(user);

My firestore data is symple like below.

collection: user
documet: uid(automatically made by firebase)
data:{
 name:'someone name',
 age:22
}

so I was expected the result like that

{
 name:'someone name',
 age:22
}

but result was 'undefined' Does anyone give me advise, please?

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

0

The onSnapshot function doesn't return a Promise, so you can't use await on it. What you're looking for is getDoc:

const snap = await getDocs(collection(db, "user"));
let user: any = [];
snap.docs.forEach((doc: any) => {
  user.push({ ...doc.data() });
});
console.log(user);

Also see the Firebase documentation on getting a document once.

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!