I've used matb33:collection-hooks
to insert a document after inserting into another, is it possible to update an existing document following an insert? I'm trying to do the following:
Box
, whose data context has an _id
of boxId
, call a method to insert a new document into Targets
collection_id
of the new document and add it to an array of the document with _id
of boxId
.Since this
refers to the new document in the hook, I can't figure out how to get the boxId
to update the right document.
Final code here per Pawel's answer:
Template.Box.events({
'click .add button': function(e) {
e.preventDefault();
var currentBoxId = this._id;
var target = {
...
};
Meteor.call('targetAdd', target, currentBoxId, function(){});
}
});
Meteor.methods({
targetAdd: function(targetAttributes, currentBoxId) {
check(this.userId, String);
check(currentBoxId, String);
check(targetAttributes, {
...
});
var target = _.extend(targetAttributes, {
userId: user._id,
submitted: new Date()
});
var targetId = Targets.insert(target);
Boxes.update(currentBoxId, {$addToSet: {targets:targetId}});
return {
_id: targetId
};
}
});
Collection hooks don't know and don't have dependency on where the document was inserted/updated (that is one of the points of collection hooks - it doesn't matter where the operation comes from, the hook should always behave the same way).
What is more, even your targetAdd method doesn't have the boxId already - you would have to pass it as one of the parameters.
So in this case, you should pass the boxId as a parameter to targetAdd method and modify the box document in the method.
Use the collection hooks only for cases when context of a collection operation is not important.