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

263
Views
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 answers
Answer question

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 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!