top*_*ace 3 javascript google-cloud-firestore
我正在YouTube上关注此Google Cloud Firestore示例,并成功获取了实时更新。但是,我不知道如何取消订阅更新,因为视频中没有对此进行解释。我阅读了文档,以创建一个unsubscribe()函数,但它对我不起作用。
getRealtimeUpdates = function(document) {
firestore.collection("collection_name")
.onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
if (doc && doc.exists) {
const myData = doc.data();
// DO SOMETHING
}
});
});
}Run Code Online (Sandbox Code Playgroud)
该firestore.collection().onSnapshot()函数返回一个退订函数。只要叫它,你就应该成为guchi。
您还可以在此处找到另一个示例:如何删除DocumentSnapshot事件的侦听器(Google Cloud FireStore)
这是我创建的应该起作用的代码段:
let unsubscribe;
getRealtimeUpdates = function(document) {
unsubscribe = firestore.collection("collection_name")
.onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
if (doc && doc.exists) {
const myData = doc.data();
// DO SOMETHING
}
});
});
}
// unsubscribe:
unsubscribe();Run Code Online (Sandbox Code Playgroud)
相应的Firebase文档的路径:
https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference#onSnapshot
返回一个取消订阅函数,可以调用该函数取消快照侦听器。
| 归档时间: |
|
| 查看次数: |
2199 次 |
| 最近记录: |