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

266
Visualizações
Can't access data() in QueryDocumentSnapshot

I'm trying to get data from a document:

await db.collection("collectionName").doc(uid).get("root").data()

but I get: 'data is not a function', which is strange, if I remove data()

await db.collection("collectionName").doc(uid).get("root")

I get the following object, and in fact I can see the root field in _fieldsProto

QueryDocumentSnapshot {
  _fieldsProto: { root: { mapValue: [Object], valueType: 'mapValue' } },
  _ref: DocumentReference {
    _firestore: Firestore {
      _settings: [Object],
.......

anyone knows how I could get fields from QueryDocumentSnapshot?

edit:

I realised I was calling data() before awaiting. The working code is:

const snapshot await db.collection("collectionName").doc(uid).get()
console.log(snapshot.data())
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I suggest starting with the documentation to learn how to get a document from Firestore. get() returns a promise which becomes fulfilled when the query is complete.

db.collection("collectionName").doc(uid).get().then(doc => {
    if (doc.exists) {
        console.log("Document data:", doc.data());
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch((error) => {
    console.log("Error getting document:", error);
});

As far as I can tell, you aren't using then or await to wait for the query to complete and get a hold of the snapshot in the callback. That isn't an optional step - the query results are not available immediately.

Also, get() does not take any arguments. If you want a field out of the snapshot, you have to dig into the snapshot provided by the callback. If your document field is called "root", then just access it like any object property:

console.log(doc.data().root)
about 4 years ago · Santiago Trujillo 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