Consider the following Firebase Firestore code to get a single document by ID:
const docRef = firestore.collection('users').doc('user123');
const docSnap = await docRef.get();
if (docSnap.exists) {
console.log(docSnap.data());
} else {
console.log('No such document!');
}What will be printed if the document with ID 'user123' exists and contains {name: 'Alice', age: 30}?
const docRef = firestore.collection('users').doc('user123'); const docSnap = await docRef.get(); if (docSnap.exists) { console.log(docSnap.data()); } else { console.log('No such document!'); }
Check what docSnap.data() returns when the document exists.
If the document exists, docSnap.data() returns the document data as an object. Otherwise, it prints 'No such document!'.
You want to get a single document from a Firestore collection by its ID. Which method should you use?
Think about how to directly access a document by its ID.
The doc('user123').get() method fetches the single document with that ID. The other options either get all documents or add a new one.
You run this code to get a document:
const docSnap = await firestore.collection('users').doc('user123').get();But your Firestore security rules deny read access to this document for your user. What will happen?
Consider how Firestore enforces security rules on reads.
If security rules deny read access, Firestore throws a permission error and does not return the document data.
You want to get a document and handle the case where it might not exist. Which snippet correctly does this?
Check the property that indicates document existence.
The exists property on the document snapshot tells if the document exists. Checking data() for null or undefined is not reliable.
You have a Firestore document that your app reads very often. Which approach best optimizes performance and cost?
Think about caching and offline capabilities Firestore offers.
Firestore supports offline persistence and local caching, which reduces network reads and costs while improving performance. Calling get() every time or duplicating data adds overhead or complexity.