-1

I have a collection in a firebase. I'm listening to changes with onSnapshot method.

If any item from a collection is changed in any way listener is fired. I am getting new data, which was inserted into my collection with docChanges method

db.collection('collection')
    .onSnapshot((snapshot) => {
        snapshot.docChanges().forEach((change) => {
            const payload = {
                id: change.doc.id,
                data: change.doc.data(),
            };

            ...... some action
        });
    });

and now... I need to compare new data (which was just inserted) with old data (from before insert) and I am wondering if there is any way to do that?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
BigKamil5
  • 295
  • 3
  • 11

1 Answers1

0

The Firestore SDK doesn't give you any ways to detect the differences between the "before" and "after" states of a document's fields. You will have to remember both snapshots and look for differences in the fields yourself. You might want to do a search for some ideas on how to go about this, for example: Generic deep diff between two objects

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441