📖 Scenario: You are building a simple app that needs to fetch user profile information stored in Firebase Firestore. You want to get the data of a single user by their unique ID.
🎯 Goal: Learn how to write code that retrieves a single document from a Firestore collection using the document ID.
📋 What You'll Learn
Create a Firestore collection reference
Set a variable with the document ID to fetch
Write the code to get the document snapshot
Access the document data safely
💡 Why This Matters
🌍 Real World
Fetching a single user profile or settings document from Firestore is common in apps that personalize content or manage user data.
💼 Career
Understanding how to retrieve single documents from Firestore is essential for frontend and backend developers working with Firebase to build real-time, scalable applications.
Progress0 / 4 steps
1
Create a Firestore collection reference
Write a line of code to create a Firestore collection reference called usersRef that points to the collection named users using firebase.firestore().collection('users').
Firebase
Hint
Use firebase.firestore().collection('users') to get the collection reference.
2
Set the document ID to fetch
Create a constant variable called userId and set it to the string 'user123', which is the ID of the user document you want to get.
Firebase
Hint
Use const userId = 'user123'; to store the document ID.
3
Write the code to get the document snapshot
Write an asynchronous function called getUserData that uses usersRef.doc(userId).get() to get the document snapshot and stores it in a variable called doc.
Firebase
Hint
Use await usersRef.doc(userId).get() inside an async function to get the document.
4
Access the document data safely
Inside the getUserData function, add code to check if doc.exists is true. If yes, create a variable called userData and set it to doc.data(). This will hold the user's data.
Firebase
Hint
Use if (doc.exists) to check if the document was found, then use doc.data() to get its contents.
Practice
(1/5)
1. What does the getDoc() function do in Firebase when working with a single document?
easy
A. It fetches the data of a specific document from the database.
B. It creates a new document in the database.
C. It deletes a document from the database.
D. It updates the fields of a document.
Solution
Step 1: Understand the purpose of getDoc()
The getDoc() function is used to retrieve data from a specific document in Firebase Firestore.
Step 2: Differentiate from other functions
Functions like setDoc() create or update documents, while deleteDoc() removes them. getDoc() only reads data.
Final Answer:
It fetches the data of a specific document from the database. -> Option A
The code uses docSnap.exists() to verify the document presence, which is true here.
Step 2: Access the name field from document data
Since the document exists, docSnap.data() returns the data object, and name is Alice.
Final Answer:
Alice -> Option B
Quick Check:
Document exists, print name field [OK]
Hint: Check exists() before accessing data() [OK]
Common Mistakes:
Not checking exists() before data()
Expecting data() to return undefined if no doc
Confusing data() with get()
4. What is wrong with the following code snippet for getting a document from Firestore?
const docSnap = await getDoc('users/user123');
if (docSnap.exists()) {
console.log(docSnap.data());
}
medium
A. You cannot use console.log with docSnap.data().
B. Missing await keyword before getDoc().
C. docSnap.exists() is not a valid method.
D. getDoc() requires a document reference, not a string path.
Solution
Step 1: Understand getDoc() argument requirements
The getDoc() function expects a document reference created by doc(), not a string path.
Step 2: Identify the error in argument type
Passing a string like 'users/user123' directly causes an error because it's not a valid document reference.
Final Answer:
getDoc() requires a document reference, not a string path. -> Option D
Quick Check:
getDoc() needs doc() ref [OK]
Hint: Always use doc() to create reference before getDoc() [OK]
Common Mistakes:
Passing string path directly to getDoc()
Forgetting to create doc reference
Assuming getDoc() accepts string paths
5. You want to fetch a user's profile document from Firestore and safely handle the case when the document does not exist. Which code snippet correctly implements this behavior?