angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

DocumentChangeAction removed never being set

Open adamkoncz opened this issue 6 years ago • 4 comments

"@angular/core": "~9.0.7", "@angular/fire": "^6.0.0", "firebase": "^7.14.2",

On snapshotChanges the observable omits documents where payload.type==='removed'

this.afFirestore.collection(`items`).snapshotChanges().subscribe(docs => {
            docs.map(a => { console.log(`payload type ${a.payload.type}`) })
          })

When adding 2 items on the console the output is

payload.type added payload.type added

removing 1 the output is payload.type added

Expected behavior is returning "removed" type

payload.type added payload.type removed

An other maybe related issue is that snapshotChanges seems caching the results. It always returns all items not just the changes. If one item is modified it keeps the modified type in subsequent snapshotChanges even if it wasn't modified again.

This is not consistent with firebase.firestore

adamkoncz avatar May 17 '20 13:05 adamkoncz

I am experiencing something similar. When listening to a collection, it emits every single item in the collection, always with type 'added'. The collection could have 3 pre-existing documents. First emit from snapshotChanges() is the 3 documents with 'added' type. If I then add a new document, the emission is 4 documents all with 'added' type...

"firebase": "^7.9.3", "@angular/fire": "^5.4.2", "@angular/core": "~8.2.14"

AveryFerrante avatar Nov 11 '20 05:11 AveryFerrante

I've done a lot of work on the latest release in terms of the actions. I believe this all to be addressed now in 6.1.2. Can you confirm?

jamesdaniels avatar Nov 27 '20 01:11 jamesdaniels

This is not fixed, im on version 6.1.5 all documentchangeaction types are still added when a new document is added

civicBoy13 avatar Jun 13 '21 20:06 civicBoy13

@jamesdaniels This still isn't fixed in 6.1.5, "added" & "modified" work as advertised, but "removed" is never set.

Checked via this code:

.snapshotChanges()
.pipe(
  map(changes => {
    const removed = changes.filter(change => change.type === "removed");
    console.log(removed.length);
  })
);

Here's my quickfix in the meantime:

startWith({} as ModelWrapper),
pairwise(),
map(([before, after]) => {
  const keysRemoved = Object.keys(before).filter(key => Object.keys(after).indexOf(key) < 0);

  const ret = { ...after };
  for (const keyRemoved of keysRemoved) {
    ret[keyRemoved] = before[keyRemoved];
    ret[keyRemoved].type = "removed";
  }
  return ret;
}),
interface ModelWrapper {
  [id: string]: DocumentChangeAction<MyModel>;
}

The query itself is nothing special, it just returns the whole collection desc-sorted by a date field.

ciriousjoker avatar Aug 21 '21 13:08 ciriousjoker