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

268
Views
Firebase: Trouble getting field value

Firestore database

Given the following data struct in firebase, I wish to retrieve the field in just standard JS. I have tried many methods of getting it, but for some reason, I cannot. I have tried .get(), forEach(), I have tried getting a snapshop, but it won't work. At the start of my JS file I do:

const auth = firebase.auth();
const db = firebase.firestore();

let totalGroups;
db.collection('totalGroups').doc('totalGroups').get().then(function(querySnapshot) {
    querySnapshot.docs.forEach(function(doc) {
        if (doc.data().totalGroups != null) {
            totalGroups = doc.data().totalGroups console.log("here is total groups" + totalGroups)
            //Total Groups is undefined out here but defined in fuction
        }
    })
})

and normally I am able to get .get() just fine. I am looking for the most simple method of getting this value. thanks.

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

0

First, you are using get() on a DocumentReference which returns a DocumentSnapshot containing data of that single document only and has no docs property on it. Try refactoring the code as shown below:

db.collection('totalGroups').doc('totalGroups').get().then(function(snapshot) {
    const docData = snapshot.data();
    console.log(docData)
})

Also do note that if you were using get() on a CollectionReference, then it would have returned a QuerySnapshot where the existing code works fine.

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!