Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get a reference to the collection named 'users'.
Firebase
const usersCollection = firestore.collection([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the collection name.
Using the wrong collection name.
✗ Incorrect
The collection name must be a string. Using quotes around 'users' is correct.
2fill in blank
mediumComplete the code to fetch all documents from the 'users' collection.
Firebase
const snapshot = await usersCollection.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'getDocs' which is from Firebase v9 modular syntax, not the older syntax.
Using 'fetch' which is not a Firestore method.
✗ Incorrect
The correct method to fetch all documents from a collection in Firebase Firestore is get().
3fill in blank
hardFix the error in the code to loop through all documents in the snapshot.
Firebase
snapshot.forEach(doc => [1](doc.data())); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'print' which is not a JavaScript function.
Using 'log' without 'console'.
✗ Incorrect
To print data to the console in JavaScript, use console.log.
4fill in blank
hardFill both blanks to create a map of document IDs to their data.
Firebase
const dataMap = {};
snapshot.forEach(doc => {
dataMap[doc.[1]] = doc.[2]();
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods like 'getId' or 'getData' which do not exist.
Confusing properties and methods.
✗ Incorrect
Each document has an id property and a data() method to get its data.
5fill in blank
hardFill all three blanks to filter documents with age greater than 18 and map their names.
Firebase
const adults = snapshot.docs .filter(doc => doc.data().[1] [2] [3]) .map(doc => doc.data().name);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong field name.
Using the wrong comparison operator.
✗ Incorrect
We filter documents where the 'age' field is greater than 18, then map to get their 'name'.