javascriptmeteorhookmeteor-collection-hooks

Add id of new document to array in existing document using collection-hooks


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:

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
        };
    }
});

Solution

  • 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.