简体   繁体   中英

Passing id returned by outer function to inner function

I have a nested function within an event handler. The outer function creates a copy of a document from the Trees collection. The inner function then creates copies of any documents from the Branches collection whose ids are included in an array in the treeBranches field of the original tree document.

I need to pass newTreeId from the outer function to the inner function, so that the new branch ids can be added to the array in the new document. console.log(newTreeID) in the inner function is currently returning undefined.

Template.Actions.events({
    'change .action-selection': function(e) {
        e.preventDefault();
        var selection = $(e.target).val();
        var currentTreeId = this._id;
        var branches = Branches.find({_id:{$in:this.treeBranches}});

        switch(selection) {
            case "repeat":
                return Meteor.call('treeRepeat', currentTreeId, function (newTreeId) {
                    branches.forEach(function(b) {
                        var currentBranchId = b._id;
                        console.log(newTreeId);
                        Meteor.call('treeBranchesRepeat', currentBranchId, newTreeId, function () {
                        });
                    });
                });
                break;
                ...

Meteor.methods({
    treeRepeat: function(currentTreeId) {
        check(currentTreeId, String);

        var tree = Trees.findOne({_id:currentTreeId}, {fields:{_id:0, treeBranches:0}});
        var newTreeId = Trees.insert(tree);

        return {
            _id: newTreeId
        };
    },
    treeBranchesRepeat: function(currentBranchId, newTreeId) {
        check(currentBranchId, String);
        check(newTreeId, String);

        var branch = Branches.findOne({_id:currentBranchId}, {fields: {_id: 0}});
        var newBranchId = Branches.insert(branch);
        Trees.update({_id:newTreeId},{$push:{treeBranches:newBranchId}});

        return {
            _id: newBranchId
        };
    }
});

I think this could be simplified by performing all duplications of the tree and branches all at a single request to the server. Suggesting the following workflow (not tested, but I think you get the idea):

Template.Actions.events({
    'change .action-selection': function(e) {
        e.preventDefault();
        var selection = $(e.target).val();
        var currentTreeId = this._id;
        var currentTreebranches = this.treeBranches;

        switch(selection) {
            case "repeat":
                return Meteor.call('treeAllRepeat', currentTreeId, currentTreebranches, function (err, res) {
                  if (err) { console.log(err); }

                  console.log(res); // Your new tree _id
                });
                break;
                ...

Meteor.methods({
    treeAllRepeat: function(currentTreeId, branchIds) {
        check(currentTreeId, String);
        check(branchIds, [String]);

        var tree = Trees.findOne({ _id: currentTreeId }, { fields: { _id: 0, treeBranches: 0 } });

        if (!tree) {
          throw new Error('Tree with id ' + currentTreeId + ' not found');
        }

        var newTreeId = Trees.insert(tree);
        var branches = Branches.find({ _id: { $in: branchIds || [] } });

        branches.forEach(function (branch) {
          var newBranch = Branches.findOne({ _id: branch._id }, { fields: { _id: 0 } });
          var newBranchId = Branches.insert(newBranch);
          Trees.update({ _id: newTreeId }, { $push: { treeBranches: newBranchId } });
        });

        return {
          _id: newTreeId
        };
    }
});

I assume this.treeBranches is the ids of the branches? If not, you probably want to do something like:

var currentTreebranches = _.pluck(this.treeBranches, '_id');

PS. Remember t subscribe to the new tree, if you have removed the autopublish package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM