Meteor-Community-Packages / meteor-collection-hooks

Meteor Collection Hooks
https://atmospherejs.com/matb33/collection-hooks
MIT License
657 stars 92 forks source link

ensure $set is not undefined #42

Closed nate-strauser closed 10 years ago

nate-strauser commented 10 years ago

if i have a hook like so:

Things.before.update(function (userId, doc, fieldNames, modifier, options) {
    modifier.$set.updatedAt = new Date();
});

an update like this works without checking/initing $set

Things.update({_id:"wAARchoDwYubfff2J"},{$set:{name:"updated"}});

but this does not

Things.update({_id:"wAARchoDwYubfff2J"},{$inc:{count:1}});

if $set is not used in the update, then it has to be initialized before it can be added to or altered

noted by AVITAL OLIVER during a review of this article - https://meteor.hackpad.com/Meteor-Cookbook-Using-Dates-and-Times-qSQCGFc06gH

matb33 commented 10 years ago

Very good point, and I was surprised to find my example didn't already do this as it's the same pattern I use in my own code! Thanks for the contribution and attention to detail