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.