Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda