1
votes

I have this problem: I'm doing a real-time query with firebase firestore in javascript, my code is as follows:

   first = db.collection ("messages")
     .where ("codeConversation", "==", codeConversation)
     .orderBy ("date", "desc")
     .limit (25);

   first.onSnapshot (function (querySnapshot) {

// .. other execution

I read the documentation of how to stop it but I do not understand how to apply it says it should be the following:

var unsubscribe = db.collection ("cities")
     .onSnapshot (function () {});
// ...
// Stop listening to changes
unsubscribe ();

I tried to replace the previous code with my own query in the following way to stop it but it did not work

var unsubscribe = db.collection ("messages")
         .where ("codeConversation", "==", codeConversation)
         .orderBy ("date", "desc")
         .limit (25) .onSnapshot (function (querySnapshot) {
     // ... other execution
 // Stop listening to changes
 unsubscribe ();
1
Does this not work? By calling the unsubscribe() function, all updates to your code should stop. - Kumar Bibek
I don't understand how apply that function, the variable is called "unsubscribe" and the unsubscribe function, the two is the same? or simply I must put the unsubcribe function when I need stop all the querys? - Emiliano Pamont

1 Answers

1
votes

The type of the variable unsubscribe is a "Subscriber". So, you can call the unsubscribe() method on that variable. The official docs are a bit misleading.

var collectionObservable = db.collection('students').doc('NnBOX9f9kvBKerGKdbAi').snapshotChanges();

var subscription = collectionObservable.subscribe(res => {
  console.log(res);
});

subscription.unsubscribe();