Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

276
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda