Struggling to set up a Firestore listener for real-time updates? Check out this snippet of code I'm working with:
db.collection('users/user')
.onSnapshot(function(snapshot) {
snapshot.docChanges().forEach(function(change) {
if (change.type === "added") {
console.log("New user: ", change.doc.data());
}
if (change.type === "modified") {
console.log("Modified user: ", change.doc.data());
const data = {
id: change.oldIndex,
name: change.doc.data().name,
surname: change.doc.data().surname
};
vm.users.push(data);
}
if (change.type === "removed") {
console.log("Removed user: ", change.doc.data());
}
});
});
Strange how the page doesn't reflect changes when the "modified" event occurs, but the console shows the updated data. Any ideas on how to tackle this issue?