You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document.
import { doc, onSnapshot } from "firebase/firestore"; const unsub = onSnapshot(doc(db, "cities", "SF"), (doc) => { console.log("Current data: ", doc.data()); });
const doc = db.collection('cities').doc('SF'); const observer = doc.onSnapshot(docSnapshot => { console.log(`Received doc snapshot: ${docSnapshot}`); // ... }, err => { console.log(`Encountered error: ${err}`); });index.js
Last updated 2 years ago
Was this helpful?