Complete the code to initialize a Firestore listener for real-time updates.
db.collection('users').[1](snapshot => { snapshot.docChanges().forEach(change => { if (change.type === 'added') { console.log('New user: ', change.doc.data()); } }); });
The onSnapshot method sets up a real-time listener to Firestore collection changes.
Complete the code to stop the Firestore listener when no longer needed.
const unsubscribe = db.collection('messages').onSnapshot(snapshot => { // handle updates }); // Later in code [1]();
The unsubscribe function returned by onSnapshot stops the listener.
Fix the error in the listener callback to correctly handle added documents.
db.collection('orders').onSnapshot(snapshot => { snapshot.docChanges().forEach(change => { if (change.type === [1]) { console.log('Order added:', change.doc.data()); } }); });
The change.type is a string and must be compared to the string 'added'.
Fill both blanks to filter Firestore documents and listen for real-time updates.
db.collection('tasks').where('status', [1], 'completed').[2](snapshot => { snapshot.forEach(doc => { console.log(doc.id, '=>', doc.data()); }); });
The where clause uses '==' to filter documents where status equals 'completed'. The onSnapshot method listens for real-time updates.
Fill all three blanks to set up a listener that logs only modified documents with priority above 5.
db.collection('alerts').where('priority', [1], 5).[2](snapshot => { snapshot.docChanges().forEach(change => { if (change.type === [3]) { console.log('Modified alert:', change.doc.data()); } }); });
The where clause uses '>' to filter priorities above 5. The onSnapshot method listens for real-time updates. The change.type is compared to the string 'modified' to detect modified documents.