Reputation: 1
how do i get the type of change that happened in onSnapshot in firebase, i want to use a if statement on the change to know the type of snapshot change if it was a ' add, remove, modified'
Upvotes: 0
Views: 543
Reputation: 83163
It is explained here in the doc. The provided example is:
import { collection, query, where, onSnapshot } from "firebase/firestore";
const q = query(collection(db, "cities"), where("state", "==", "CA"));
const unsubscribe = onSnapshot(q, (snapshot) => {
snapshot.docChanges().forEach((change) => {
if (change.type === "added") {
console.log("New city: ", change.doc.data());
}
if (change.type === "modified") {
console.log("Modified city: ", change.doc.data());
}
if (change.type === "removed") {
console.log("Removed city: ", change.doc.data());
}
});
});
Upvotes: 1