0
0
Firebasecloud~10 mins

Getting a single document in Firebase - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A'db'
B'getDoc'
C'users'
D'userId123'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the collection name instead of the document ID.
Passing the database object instead of the document ID.
2fill in blank
medium

Complete 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'
AdocRef
BgetDoc
CdocSnap
Ddb
Attempts:
3 left
💡 Hint
Common Mistakes
Calling exists() on the document reference instead of the snapshot.
Using the database object instead of the snapshot.
3fill in blank
hard

Fix 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'
AgetDoc
BdocGet
CfetchDoc
DgetDocument
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like getDocument or fetchDoc.
Misspelling the function name.
4fill in blank
hard

Fill 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'
Adoc
BgetDoc
C'productId456'
D'products'
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.
5fill in blank
hard

Fill 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'
AdocSnap
BdocRef
Dexists
Attempts:
3 left
💡 Hint
Common Mistakes
Calling exists() on the document reference.
Logging data from the reference instead of the snapshot.