Minderov
Minderov

Reputation: 521

Meteor observeChanges(). How to check the actual changes?

I have a code that looks like this

Trades.find().observeChanges({
    changed: function(id, fields) {
        // do stuff
    }
});

Where each Trades has an array of items inside

TradesSchema = new SimpleSchema({
    // ...
    items: {
        type: [String]
    },
    // ...
});

Trades.attachSchema(TradesSchema);

These items are being changed sometimes, and I want to track the changes. The code works fine, except that in fields it returns all the items, not only the items that were changed.

Is there any way to see which exactly item was changed without changing the structure of the collection?

Upvotes: 1

Views: 1783

Answers (1)

Minderov
Minderov

Reputation: 521

Thanks @Season for the hint!


observeChanges only gives the new values, so you have to use observe, since it returns both the new and old documents. Then you need to compare them to see what exactly got changed. (See docs for observe on meteor.com)

Trades.find().observe({
    changed: function(newDocument, oldDocument) {
        // compare newDocument to oldDocument to find out what has changed
    }
});

Upvotes: 1

Related Questions