0
0
GcpHow-ToBeginner · 4 min read

How to Read a Document from Firestore in Google Cloud

To read a document from Firestore, use the get() method on a document reference obtained via doc(). This fetches the document snapshot, from which you can access data with data() or check existence with exists.
📐

Syntax

To read a document from Firestore, first get a reference to the document using doc(). Then call get() on that reference to fetch the document snapshot. Check if the document exists with exists, and access its data with data().

  • doc(path): Gets a reference to the document at the given path.
  • get(): Fetches the document snapshot asynchronously.
  • exists: Boolean indicating if the document exists.
  • data(): Returns the document data as an object.
javascript
const docRef = firestore.doc('collection/documentId');
const docSnap = await docRef.get();
if (docSnap.exists) {
  const data = docSnap.data();
  // use data
} else {
  // document does not exist
}
💻

Example

This example shows how to read a document from Firestore using the Firebase Admin SDK in Node.js. It prints the document data if found, or a message if not.

javascript
import { initializeApp, applicationDefault } from 'firebase-admin/app';
import { getFirestore } from 'firebase-admin/firestore';

initializeApp({
  credential: applicationDefault(),
});

const firestore = getFirestore();

async function readDocument() {
  const docRef = firestore.doc('users/alice');
  const docSnap = await docRef.get();

  if (docSnap.exists) {
    console.log('Document data:', docSnap.data());
  } else {
    console.log('No such document!');
  }
}

readDocument();
Output
Document data: { name: 'Alice', age: 30, email: 'alice@example.com' }
⚠️

Common Pitfalls

Common mistakes when reading Firestore documents include:

  • Not awaiting the get() call, causing undefined data.
  • Assuming the document exists without checking exists.
  • Using incorrect document paths.
  • Not handling errors from network or permission issues.

Always use await with get() and check exists before accessing data.

javascript
/* Wrong way: Missing await and no existence check */
const docRef = firestore.doc('users/alice');
const docSnap = await docRef.get(); // Added await
if (docSnap.exists) {
  console.log(docSnap.data());
} else {
  console.log('Document not found');
}

/* Right way: Await and check existence */
const docSnapCorrect = await docRef.get();
if (docSnapCorrect.exists) {
  console.log(docSnapCorrect.data());
} else {
  console.log('Document not found');
}
📊

Quick Reference

Remember these key points when reading Firestore documents:

  • Use doc(path) to get a document reference.
  • Always await the get() call.
  • Check exists before accessing data.
  • Use data() to get the document content.
  • Handle errors with try-catch or promise catch.

Key Takeaways

Always await the get() method to fetch the document snapshot.
Check the exists property before accessing document data.
Use doc(path) to get a reference to the specific document.
Handle errors and missing documents gracefully in your code.
Access document fields using the data() method on the snapshot.