I'm making a web game with Firestore. I use the Firestore listener and want to detach it.
The official document shows this code:
var unsubscribe = db.collection("cities")
.onSnapshot(function (){
// Respond to data
// ...
});
// Later ...
// Stop listening to changes
unsubscribe();
But in this way, when detaching listener, nameless function works again. So I can't do this.
var name = "New York"
var unsubscribe = db.collection("cities")
.onSnapshot(function (doc){
var data = doc.data();
if(data.name == name){
// Stop listening to changes
unsubscribe()
}
});
If I do this, infinite loop occurs because unsubscribe()
calls unsubscribe()
. How can I detach listener without unsubscribe()
working?