Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get a single document from Firestore.
Firebase
const docRef = doc(db, 'users', [1]); const docSnap = await getDoc(docRef);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the collection name instead of the document ID.
Passing the database object instead of the document ID.
✗ Incorrect
You need to specify the document ID to get a single document. Here, 'userId123' is the document ID.
2fill in blank
mediumComplete the code to check if the document exists.
Firebase
if ([1].exists()) { console.log('Document data:', docSnap.data()); } else { console.log('No such document!'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling exists() on the document reference instead of the snapshot.
Using the database object instead of the snapshot.
✗ Incorrect
The exists() method is called on the document snapshot object, which is docSnap.
3fill in blank
hardFix the error in the code to correctly import the Firestore functions.
Firebase
import { [1] } from 'firebase/firestore';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like getDocument or fetchDoc.
Misspelling the function name.
✗ Incorrect
The correct function to import for getting a document is getDoc.
4fill in blank
hardFill both blanks to create a document reference and get the document snapshot.
Firebase
const docRef = [1](db, 'products', [2]); const docSnap = await getDoc(docRef);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using collection name instead of document ID in the second blank.
Using getDoc instead of doc for the first blank.
✗ Incorrect
Use doc to create a document reference and provide the document ID as the third argument.
5fill in blank
hardFill all three blanks to check if a document exists and log its data.
Firebase
if ([1].[2]()) { console.log('Data:', [3].data()); } else { console.log('No document found'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling exists() on the document reference.
Logging data from the reference instead of the snapshot.
✗ Incorrect
You check if the document snapshot docSnap exists by calling exists(), then log its data.