mcarriere
mcarriere

Reputation: 389

Firestore snapshotChanges DocumentChangeType "removed" never sent

I'm querying a Firestore collection using a where clause and am subscribed on snapshotChanges. According to the documentation I should get an event for each elements that are added, modified and removed.

I get the added and modified documents fine but I never get the removed ones.

Here's an example of my code:

this.db
      .collection<Test>('test', ref => ref.where('someid', '==', someid))
      .snapshotChanges()
      .subscribe(async snapshot => {
        for (const element of snapshot) {
          switch (element.type) {
            case 'added':
              // Works fine
              break;
            case 'modified':
              // Works fine
              break;
            case 'removed':
              // Never triggered
              break;
          }
        }
      });

I'm assuming it's because of the fact i'm using a where. Has anyone encountered this?

UPDATE: 2020-10-14

After more testing, it appears to be an issue only with AngularFirestore and not Firestore itself. In the meantime as a workaround I use AngularFirestore.firestore.onSnapshot directly.

UPDATE: 2020-11-04 Turns out with AngularFirestore you need to use stateChanges to get the behavior I was looking for. Hope this can help someone.

Upvotes: 5

Views: 857

Answers (3)

Dheeraj Tiwari
Dheeraj Tiwari

Reputation: 31

This is happen when you deleted the last item of the collection, at this point of time compiler triggered at empty or null block.

Upvotes: 0

mcarriere
mcarriere

Reputation: 389

Turns out with AngularFirestore you need to use stateChanges to get the behavior I was looking for.

Here's the documentation that explains the difference between snapshotChanges and stateChanges: https://github.com/angular/angularfire/blob/master/docs/firestore/collections.md

Upvotes: 2

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

A listener will receive removed events for documents that are removed from the query results while that listener is active.

So in your case, that means you'll get the event if:

  1. The listener is active, subscribed to the query, and.
  2. The document is removed from the collection, or
  3. The value of someid field in he document changes.

Upvotes: 1

Related Questions