Complete the code to listen for real-time updates on a Firestore document.
const unsubscribe = docRef.[1](doc => { console.log("Current data:", doc.data()); });
The onSnapshot method sets up a real-time listener that triggers whenever the document changes.
Complete the code to listen for real-time updates on a Firestore collection.
const unsubscribe = collectionRef.[1](snapshot => {
snapshot.docs.forEach(doc => {
console.log(doc.id, doc.data());
});
});onSnapshot listens to changes in the collection and provides a snapshot of documents.
Fix the error in the code to correctly unsubscribe from the listener.
const unsubscribe = docRef.onSnapshot(doc => {
console.log(doc.data());
});
// Later in code
[1]();The unsubscribe variable holds the function to stop listening. Calling unsubscribe() stops the listener.
Fill both blanks to correctly handle errors in a real-time listener.
const unsubscribe = docRef.onSnapshot([1], [2]);
The first argument is the callback for data updates, the second handles errors.
Fill all three blanks to set up a real-time listener with options to include metadata changes.
const unsubscribe = collectionRef.onSnapshot([1], [2], [3]);
The first callback handles data, the second handles errors, and the third is options to include metadata changes.