Updating a sub-document through meteor methods via a click function proving problematic

感情迁移 提交于 2019-12-25 10:58:33

问题


I have a button .toggle-addToSet that captures two integers, this.id which is the current posts' id, and setid (sid) which is an _id of a collection Set which the user has the ability to create as many as they want. The goal is updating a chosen Set of a given _id sid with this.id. It looks like this in the js

Template.latestSingle.events({
  'click .toggle-addToSet': function(e, template) {
    var ob = this.id
    console.log(ob);
    var sid = $(e.currentTarget).data('setid');
    Meteor.call('addingSets', ob, sid, function(error, user) {
      console.log(ob)
    });
  }
});

What's going on is that ob is the id of a single document in an array, this document is a post, so I'm capturing that post.

Within each post context is a modal which brings about a collection called Sets which has a sub-document array called ArticleId that can be updated by the user by, inserting this.id (ob) via the click function with the button toggle-addToSet as seen above.

The user Creates the Set with a title such as Business or Lifestyle and when they create it, they can save post ids in an array called ArticleId whenever they find a post they would like to add. Think Pinterest Boards or G+ collections.

var sid = $(e.currentTarget).data('setid'); is the _id of each Set which the use selects to add an article into.

The idea is to add this.id (ob) into the chosen Set through that Set's _id sid. Each Set looks like this

So my methods is like this

Meteor.methods({

    addingSets: function(set, sid, ob) {
        console.log(sid);
        console.log(ob);
        Sets.update({
      _id: sid
    },
    {
      $addToSet: {

        ArticleId: ob
      }
    });
    }
});

However this is not working, I cannot seem to be able to update it. I can do it manually by typing it through a form, but when it comes to updating it via the click function, it's not working.

When I do console.log(sid); in my server, methods I get the correct Set _id in my terminal.
When I do console.log(ob); in my server, methods I get unidentified in my terminal. But in my client, it's logging the correct this.id so there is a disconnect somewhere and I'm not sure how to handle that.


回答1:


You have an extra parameter in your addingSets method.

Currently you have addingSets: function(set, sid, ob) defined in the function.

When you're calling it from the client, you're doing it like so:

Meteor.call('addingSets', ob, sid, function(error, user) {...}

Notice that the function is expecting 3 parameters to be passed to it while you're giving it only 2. So in your case, ob gets mapped to set, and sid gets mapped to sid and since the 3rd param isn't being passed, it's undefined.

Helper:

Template.latestSingle.events({
  'click .toggle-addToSet': function(e, template) {
    var ob = this.id
    console.log(ob);
    var sid = $(e.currentTarget).data('setid');
    Meteor.call('addingSets', ob, sid, function(error, user) {
      console.log(ob)
    });
  }
});

Server:

Meteor.methods({

    addingSets: function(ob, sid) {
        console.log(sid);
        console.log(ob);
        Sets.update({
      _id: sid
    },
    {
      $addToSet: {

        ArticleId: ob
      }
    });
    }
});

The positions and the params passed are important.



来源:https://stackoverflow.com/questions/44273891/updating-a-sub-document-through-meteor-methods-via-a-click-function-proving-prob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!