angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

[Bug] Collection not updating properly on document deletes

Open muffinrecon opened this issue 9 years ago • 15 comments

I am subscribed to a collection using the async pipe, however when I go to delete a document inside the collection, any object below the deleted document gets removed from the UX and the collection observable isn't updated.

When I delete from the firebase database console, it works as intended

Thanks!

muffinrecon avatar Nov 25 '17 00:11 muffinrecon

Do you use .limit()? Or query more than once to the same path? It might be the similar to what happens to me https://github.com/angular/angularfire2/issues/1356

diegofin avatar Nov 28 '17 12:11 diegofin

No i'm not using any queries/limits. Just pulling the collection and calling snapshotChanges() to get the document ids with the docs.

I believe we are having the same issue. For me to force the update, I just call the collection again.

muffinrecon avatar Nov 28 '17 19:11 muffinrecon

@muffinrecon Can you use the issue template and create a stackblitz? That will be much easier to debug.

davideast avatar Nov 28 '17 21:11 davideast

Any update on this? I am having the same issue. When a document is deleted, a new value is emitted within the snapshotChanges() subscription. Only, the DocumentChangeAction[] is empty. There is no 'removed' action.

jkossis avatar Dec 14 '17 18:12 jkossis

Hmmm... I'll take a peek here. If someone can put a repro together on stackblitz it would be appreciated.

jamesdaniels avatar Dec 14 '17 20:12 jamesdaniels

Is this Firestore or RTDB?

jamesdaniels avatar Dec 14 '17 20:12 jamesdaniels

I think is the same bug than this one https://github.com/angular/angularfire2/issues/1356, there are 2 stackblitzs

diegofin avatar Dec 14 '17 21:12 diegofin

^ I think it is a different bug. I switched over to stateChanges, and I now receive 'removed' events. It seems like snapshotChanges does not provide 'remove' events.

jkossis avatar Dec 15 '17 13:12 jkossis

Closing this one as due to lack of information and inactivity.

@jkossis snapshotChanges emits the state of your collection, so it will not emit a "removed" event. Only the array without the item removed. stateChanges seems like what you want since it is the stream of events as they occur.

davideast avatar Jan 23 '18 17:01 davideast

Hi @davideast, @jamesdaniels. Could you help me please ? i think i have a similar issue... I get different results when I delete a few objects. Do I have to get the same result?

Could you check the link below ? Stackblitz link

this.afs.collection('items', ref => ref.orderBy('date', 'desc'))
    .snapshotChanges().map(actions => {
      return actions.map(action => {
        const data = action.payload.doc.data();
        const _id = action.payload.doc.id;
        return {_id,...data};
      });
    });

this.afs.collection('items', ref => ref.orderBy('date', 'desc')).valueChanges();

results image: image

YamanSehzade avatar Feb 07 '18 23:02 YamanSehzade

@YamanSehzade Did you manage to resolve this issue? I am facing almost exactly the same.

luke-rogers avatar Apr 04 '18 07:04 luke-rogers

@Luke-Rogers Unfortunately, no

YamanSehzade avatar Apr 04 '18 10:04 YamanSehzade

@YamanSehzade, @Luke-Rogers In the future try not to respond to closed posts, just open a new bug so we'll see it. It get a lot of notifications from Github so I don't always see responses to closed issues.

For your case your problem was that you were creating multiple observables and subscribing them to the view. This is because you created your Observables with functions rather than properties. Each time you click "load items" or even run change detection, you're creating a new observable which will cause race conditions in the view.

In general you should treat your observables are properties instead of functions. They are a stream of updating content, you don't need to call a function to create one to unwrap more data. Just create the observable as a property, tack on your operators, and subscribe.

See the forked version I created for a fix: https://stackblitz.com/edit/firebase-delete-issue-enpcws?file=app%2Fhome.component.ts

davideast avatar Apr 04 '18 11:04 davideast

@davideast Unfortunately your fork does not seem to fix the issue. For example, if you add until around 5 items are in the list, then start deleting them, you will see that sometimes, two are removed from the UI until you refresh.

Happy to open this as a new issue if necessary.

luke-rogers avatar Apr 04 '18 16:04 luke-rogers

I noticed the same thing. For example in my app I have enablePersistence set to true and if I delete a document in the firebase console, that document is still in the cache and get loaded whenever I checked for snapshotChanges

pdemilly avatar Aug 28 '19 17:08 pdemilly