📖 Scenario: You are building a simple app that shows a list of books stored in a Firebase Firestore database. You want to get all the books from the books collection to display them.
🎯 Goal: Learn how to write code that fetches all documents from the books collection in Firebase Firestore.
📋 What You'll Learn
Create a reference to the books collection
Create a variable to hold the Firestore database instance
Write an async function to get all documents from the books collection
Use the getDocs method to fetch the documents
Store the fetched documents in a variable
💡 Why This Matters
🌍 Real World
Fetching all documents from a collection is a common task in apps that display lists of items like books, products, or users.
💼 Career
Knowing how to query Firestore collections is essential for frontend and backend developers working with Firebase to build real-time and scalable applications.
Progress0 / 4 steps
1
Set up Firestore database instance
Create a constant called db and set it to getFirestore() to get the Firestore database instance.
Firebase
Hint
Use const db = getFirestore() to get the Firestore database instance.
2
Create a reference to the books collection
Create a constant called booksCollection and set it to collection(db, "books") to refer to the books collection in Firestore.
Firebase
Hint
Use collection(db, "books") to get the reference to the books collection.
3
Write an async function to get all documents
Write an async function called getAllBooks that uses getDocs(booksCollection) to fetch all documents from the booksCollection. Store the result in a constant called querySnapshot.
Firebase
Hint
Use async function getAllBooks() and inside it const querySnapshot = await getDocs(booksCollection).
4
Extract and store documents data
Inside the getAllBooks function, create a constant called books that uses querySnapshot.docs.map(doc => doc.data()) to get an array of all book data objects.
Firebase
Hint
Use const books = querySnapshot.docs.map(doc => doc.data()) to get all book data.
Practice
(1/5)
1. What does the getDocs function do in Firebase when used with a collection reference?
easy
A. It creates a new document in the collection.
B. It deletes all documents in the collection.
C. It updates all documents in the collection.
D. It fetches all documents from the specified collection.
Solution
Step 1: Understand the purpose of getDocs
The getDocs function is designed to retrieve documents from a collection in Firebase.
Step 2: Identify the correct action
Using getDocs with a collection reference returns all documents inside that collection.
Final Answer:
It fetches all documents from the specified collection. -> Option D
Quick Check:
getDocs fetches documents = A [OK]
Hint: Remember: getDocs reads all documents in a collection [OK]
Common Mistakes:
Confusing getDocs with delete or update functions
Thinking getDocs creates documents
Assuming getDocs fetches a single document
2. Which of the following is the correct syntax to get all documents from a collection named users in Firebase?
easy
A. const querySnapshot = await getDocs(collection(db, 'users'));
B. const querySnapshot = getDocs('users');
C. const querySnapshot = await getDocs('db.users');
D. const querySnapshot = collection(db, 'users').getDocs();
Solution
Step 1: Identify correct function usage
The getDocs function requires a collection reference, which is created by collection(db, 'users').
Step 2: Check for correct async usage
Since getDocs returns a promise, it should be awaited with await.
Final Answer:
const querySnapshot = await getDocs(collection(db, 'users')); -> Option A
Quick Check:
Correct syntax uses await and collection() [OK]
Hint: Use await getDocs(collection(db, 'collectionName')) [OK]
Common Mistakes:
Passing collection name as string directly to getDocs
Calling getDocs as a method on collection()
Missing await for async call
3. Given the following code snippet, what will be logged to the console?
A. Missing await before getDocs, so snapshot is a Promise, not data.
B. Using collection() incorrectly; it should be collection('orders', db).
C. forEach is not a valid method on snapshot.
D. doc.id does not exist on document snapshots.
Solution
Step 1: Check async usage of getDocs
The getDocs function returns a Promise, so it must be awaited to get the snapshot.
Step 2: Understand consequences of missing await
Without await, snapshot is a Promise object, which does not have a forEach method, causing an error.
Final Answer:
Missing await before getDocs, so snapshot is a Promise, not data. -> Option A
Quick Check:
Always await getDocs to get data [OK]
Hint: Always await getDocs to get actual documents [OK]
Common Mistakes:
Forgetting to await getDocs
Incorrect order of parameters in collection()
Assuming forEach works on Promise
5. You want to get all documents from the tasks collection but only log the documents where the field completed is true. Which code snippet correctly does this?
hard
A. const querySnapshot = await getDocs(collection(db, 'tasks'));
querySnapshot.forEach(doc => {
if (doc.completed === true) {
console.log(doc.id, doc.data());
}
});
B. const querySnapshot = await getDocs(collection(db, 'tasks'));
querySnapshot.forEach(doc => {
if (doc.data().completed === true) {
console.log(doc.id, doc.data());
}
});
C. const querySnapshot = await getDocs(collection(db, 'tasks'));
querySnapshot.forEach(doc => {
if (doc.data.completed) {
console.log(doc.id, doc.data());
}
});
D. const querySnapshot = await getDocs(collection(db, 'tasks'));
querySnapshot.forEach(doc => {
if (doc.data('completed') === true) {
console.log(doc.id, doc.data());
}
});
Solution
Step 1: Access document data correctly
Use doc.data() to get the document's data object, then check the completed field.
Step 2: Filter documents by completed field
Check if doc.data().completed === true before logging the document.
Final Answer:
Code snippet that checks doc.data().completed === true and logs matching docs. -> Option B
Quick Check:
Use doc.data() to access fields [OK]
Hint: Check fields inside doc.data() for filtering [OK]
Common Mistakes:
Trying to access fields directly on doc instead of doc.data()
Using incorrect syntax like doc.data.completed
Passing field name to doc.data()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.