当查询的数据发生更改时,将在
QuerySnapshot
. 但在那张快照里是关于发生了什么变化的信息。如果只想对新添加的文档执行某些操作,请检查该文档是否使用此
sample from the documentation
:
db.collection("cities").where("state", "==", "CA")
.onSnapshot(function(snapshot) {
snapshot.docChanges().forEach(function(change) {
if (change.type === "added") {
console.log("New city: ", change.doc.data());
// This is equivalent to child_added
}
if (change.type === "modified") {
console.log("Modified city: ", change.doc.data());
// This is equivalent to child_changed
}
if (change.type === "removed") {
console.log("Removed city: ", change.doc.data());
// This is equivalent to child_removed
}
});
});
请注意,此侦听器的未修改数据是从本地缓存读取的,因此不必从服务器重新读取。