通过点击功能通过流星方法更新子文档证明有问题 [英] Updating a sub-document through meteor methods via a click function proving problematic
问题描述
我有一个按钮 .toggle-addToSet
捕获两个整数,this.id
是当前帖子的 id 和 setid (sid)
它是集合 Set
的 _id
,用户可以根据需要创建任意数量的集合.目标是使用 this.id
更新给定 _id
sid
的选定 Set
.在js中看起来像这样
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)
});
}
});
发生的事情是 ob
是数组中单个文档的 id
,这个文档是一个帖子,所以我正在捕获那个帖子.
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.
在每个帖子上下文中都有一个模态,它带来一个名为 Sets 的集合,该集合具有一个名为 ArticleId
的子文档数组,用户可以通过插入来更新它this.id
(ob)
通过带有按钮 toggle-addToSet
的点击功能,如上所示.
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.
用户使用商业或生活方式等标题创建Set
,当他们创建它时,他们可以将帖子ID保存在每当他们找到想要添加的帖子时,名为 ArticleId
的数组.想想 Pinterest Boards 或 G+ 合集.
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');
是每个Set 的_id,用户选择向其中添加文章.
var sid = $(e.currentTarget).data('setid');
is the _id of each Set which the use selects to add an article into.
想法是通过Set的_id sid
将this.id (ob)
添加到选择的Set中.每套看起来像这样
The idea is to add this.id (ob)
into the chosen Set through that Set's _id sid
. Each Set looks 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.
当我在我的服务器中执行 console.log(sid);
时,我会在终端中获取正确的 Set _id
方法.
当我在服务器中执行 console.log(ob);
时,我在终端中获得 unidentified
的方法.但是在我的客户端中,它记录了正确的 this.id,因此某处断开连接,我不确定如何处理.
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.
推荐答案
您的 addingSets
方法中有一个额外的参数.
You have an extra parameter in your addingSets
method.
目前你已经在函数中定义了addingSets:function(set, sid, ob)
.
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) {...}
请注意,该函数期望将 3 个参数传递给它,而您只给它 2 个.因此,在您的情况下,ob
被映射到 set
,并且 sid
被映射到 sid
并且由于第三个参数没有被传递,它是未定义的.
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.
助手:
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)
});
}
});
服务器:
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.
这篇关于通过点击功能通过流星方法更新子文档证明有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!