kevinharvey
kevinharvey

Reputation: 858

Meteor update timestamp

I've successfully implemented a "created" time stamp using the UMFAQ (pronounced um-fack), but I can't get the "updated" timestamp working (which the UMFAQ alludes to but does not provide a code sample for).

Here's my code:

Posts.deny({
    insert: function (userId, doc) {
        doc.created = new Date(); // timestamp
        return false;
    },
    update: function (userId, doc, fieldNames, modifier) {
        doc.updated = new Date(); // timestamp
        return false;
    }
})

When I insert an object into the collection via the Chrome console, I get a "created" timestamp. However, when I update that record (using $set) I don't get an "updated" field.

Upvotes: 2

Views: 505

Answers (1)

Tarang
Tarang

Reputation: 75945

You need to alter the modifier for an update :

Posts.deny({
    ....
    update: function (userId, doc, fieldNames, modifier) {
        if(modifier.$set) {
            modifier.$set.updated = new Date();
        } 
        return false;
    }
});

The check to see if $set is there is to prevent an error in case the client were to send an update up without using $set.

Upvotes: 4

Related Questions