Permadi
Permadi

Reputation: 55

Get previous data on realtime updates with Cloud Firestore

How to get previous data on realtime updates with firestore?

db.collection("cities").where("state", "==", "CA")
.onSnapshot(function(snapshot) {
    snapshot.docChanges().forEach(function(change) {
        if (change.type === "added") {
            console.log("New city: ", change.doc.data());
        }
        if (change.type === "modified") {
            //I need previous data here
            console.log("Modified city: ", change.doc.data());
        }
        if (change.type === "removed") {
            console.log("Removed city: ", change.doc.data());
        }
    });
});

Any help will be highly appreciated. Thank you.

Upvotes: 2

Views: 504

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

snapshot.docs always contains the entire set of documents that are currently matched by a query, so it will contain every document added and modified. But if a document is deleted from the query, it will no longer be there. You will have to make a record of those on your own. The SDK will not do that for you.

Upvotes: 1

Related Questions