Challenge - 5 Problems
Firestore Collection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Retrieve all documents from a Firestore collection
What will be the output of this Firebase Firestore query code snippet?
Firebase
const querySnapshot = await getDocs(collection(db, "users")); const users = []; querySnapshot.forEach((doc) => { users.push({ id: doc.id, ...doc.data() }); }); console.log(users);
Attempts:
2 left
💡 Hint
Remember that getDocs returns a snapshot of all documents in the collection.
✗ Incorrect
The getDocs function fetches all documents in the specified collection. The forEach loop iterates over each document snapshot, extracting the id and data, and pushes them into an array. The console.log then outputs the array of user objects.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in Firestore document retrieval
Which option contains a syntax error when trying to get all documents from a Firestore collection?
Firebase
const querySnapshot = await getDocs(collection(db, "products")); const products = []; querySnapshot.forEach(doc => { products.push(doc.data()); }); console.log(products);
Attempts:
2 left
💡 Hint
Check the syntax of the forEach loop in option D.
✗ Incorrect
Option B uses incorrect syntax for the forEach loop. The arrow function syntax requires parentheses around parameters or braces with an arrow. The correct syntax is either forEach(doc => { ... }) or forEach(function(doc) { ... }).
❓ optimization
advanced2:00remaining
Optimize fetching all documents with minimal memory usage
Which option is the best way to fetch all documents from a Firestore collection while minimizing memory usage?
Attempts:
2 left
💡 Hint
Think about fetching documents in smaller chunks instead of all at once.
✗ Incorrect
Fetching all documents at once with getDocs can consume a lot of memory if the collection is large. Using queries with limit allows fetching documents in smaller batches, reducing memory usage and improving performance.
🧠 Conceptual
advanced2:00remaining
Understanding Firestore document snapshots
What does each document snapshot represent when you get all documents from a Firestore collection?
Attempts:
2 left
💡 Hint
Think about what data you get when you call getDocs once.
✗ Incorrect
When you use getDocs, Firestore returns a snapshot of the documents as they exist at the time of the query. This snapshot is static and does not update unless you query again or use real-time listeners.
🔧 Debug
expert2:00remaining
Debug why no documents are returned from a Firestore collection
You run this code to get all documents from the 'orders' collection but get an empty array. What is the most likely reason?
Firebase
const querySnapshot = await getDocs(collection(db, "orders")); const orders = []; querySnapshot.forEach(doc => { orders.push(doc.data()); }); console.log(orders);
Attempts:
2 left
💡 Hint
Check if the collection actually has documents.
✗ Incorrect
If the collection is empty or does not exist, getDocs returns an empty snapshot, so the forEach loop does not run and the array remains empty.