0
0
Firebasecloud~10 mins

Getting a single document in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Getting a single document
Start
Call getDoc()
Check if document exists
Return data
End
This flow shows how to get one document from Firebase Firestore: call getDoc(), check if it exists, then return data or handle missing document.
Execution Sample
Firebase
const docRef = doc(db, "users", "user123");
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
  console.log(docSnap.data());
} else {
  console.log("No such document!");
}
This code tries to get a user document by ID and logs its data if found, or a message if not.
Process Table
StepActionEvaluationResult
1Create document reference for 'users/user123'docRef points to 'users/user123'docRef ready
2Call getDoc(docRef)Fetch document from FirestoredocSnap received
3Check docSnap.exists()Does document exist?True or False
4If True, call docSnap.data()Retrieve document dataData object returned
5If False, log 'No such document!'Handle missing documentMessage logged
💡 Execution stops after logging data or 'No such document!' message
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
docRefundefinedReference to 'users/user123'Reference unchangedReference unchangedReference unchanged
docSnapundefinedundefinedDocument snapshot objectDocument snapshot objectDocument snapshot object
dataundefinedundefinedundefinedundefinedDocument data or undefined
Key Moments - 2 Insights
Why do we check docSnap.exists() before accessing data?
Because the document might not exist. Checking docSnap.exists() (see step 3) prevents errors when calling docSnap.data() on a missing document.
What happens if the document does not exist?
The code logs 'No such document!' (step 5). No data is returned because docSnap.exists() is false.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of step 3 if the document exists?
AdocSnap.exists() returns true
BdocSnap.exists() returns false
CdocSnap.data() returns null
DgetDoc() throws an error
💡 Hint
Check the 'Evaluation' and 'Result' columns in step 3 of the execution table.
At which step do we get the actual document data?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the step where docSnap.data() is called in the execution table.
If the document does not exist, what will be logged?
A"Document data printed"
BAn error message
C"No such document!"
DNothing is logged
💡 Hint
See the action and result in step 5 of the execution table.
Concept Snapshot
Getting a single document in Firebase Firestore:
- Create a document reference with doc(db, collection, id)
- Use await getDoc(docRef) to fetch
- Check docSnap.exists() before accessing data
- Use docSnap.data() to get document fields
- Handle missing documents gracefully
Full Transcript
To get a single document from Firebase Firestore, first create a reference to the document using doc(db, collection, id). Then call getDoc() with this reference and wait for the result. Check if the document exists using docSnap.exists(). If it does, get the data with docSnap.data(). If not, handle the missing document by showing a message or other logic. This prevents errors and ensures your app behaves correctly when documents are missing.