使用它更新集合中的嵌套数组's索引
Updating a nested array in a collection with it's index
我是MeteorJs的新手,我很喜欢它。但我是,我正在开发一款流星Js应用程序,它必须有意见(是或否)并投票。该结构使得问题(主帖子)是评论的父级,评论是父级点赞,即问题(对象)>是或否(数组)>点赞(数组)所以,我有这个帖子的收集结构(在这种情况下的问题)
{
"_id" : "s2QBCnv6fXv5YbjAP",
"question" : "Is this real change?",
"createdAt" : ISODate("2016-05-13T21:05:23.381Z"),
"yes" : [
{
"heading" : "Yes It is",
"body" : "I think this government knows what they are doing. That's why there has not been any form of protest",
"email" : "I think this government knows what they are doing. That's why there has not been any form of protest",
"createdAt" : ISODate("2016-05-13T21:08:25.119Z"),
"replies" : [ ],
"likes" : [
"sdfsd6sd556shsdbdjs88s",
"sdfsd6sd556shsdbdjkhj88s",
"the_use_id",
"the_use_id",
"the_use_id"
]
},
{
"heading" : "Well, Yes",
"body" : "I think this is change as we all want to know what the government is doing and I am grateful to be alive at this time",
"email" : "I think this is change as we all want to know what the government is doing and I am grateful to be alive at this time",
"createdAt" : ISODate("2016-05-13T21:10:47.123Z"),
"replies" : [ ],
"likes" : [ ]
}
],
"no" : [
{
"heading" : "Not at All",
"body" : "This is not the change I wanted. This is waste of four years and I amm waiting to see the promised change",
"email" : "kenshin@kay.com",
"createdAt" : ISODate("2016-05-13T21:12:58.977Z"),
"replies" : [ ],
"likes" : [ ]
}
],
"author" : "admin",
"image" : "/cfs/files/QuestionImages/DzdpK6NdurZMTwAse"
}
我想要的是:写一个助手,当点击点赞按钮时,将loggedIn用户的ID推入点赞数组。我写了这个助手
"click #like_yes_comment": function(event){
event.preventDefault();
var questionId = Template.parentData(1)._id;
Questions.update(
{_id: questionId, , 'yes.heading':this.heading},
{$push: {'yes.0.likes': "the_use_id"}}
);
}
但是我收到这个错误
Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]
然后我把它重新编辑成这个
"click #like_yes_comment": function(event){
event.preventDefault();
var questionId = Template.parentData(1)._id;
Questions.update(
{_id: questionId},
{$push: {'yes.0.likes': "the_use_id"}}
);
}
然后它工作,但它只更新第一个"赞"数组。
所以,这是我的问题。如何更新问题>是/否>点赞数组中的每个点赞。感谢
我认为你应该在你的yes数组中生成额外的id
"yes" : [
{
id : Random.id(),
heading : "..."
...
然后使用位置$operator,因此:
Questions.update(
{_id: questionId, "yes.id": yesId},
{$push: {'yes.$.likes': userId}}
);
相关文章:
- 按最大值获取数组索引
- 删除和重用数组索引
- 警报数组索引和值
- .fadeIn .fadeOut 会导致数组索引出现奇怪的行为
- Javascript原型通用Enquries和通过数组索引分配Id
- 获取数组索引的值
- Javascript-将值与关联数组索引进行比较
- Javascript-在数组索引中循环的问题
- JavaScript - “规范化”数组索引
- 数组索引混淆
- 使用 Javascript/React.js 查找对象的数组索引
- 如何拆分数组索引
- 获取新推送元素的数组索引
- 在 javascript 中将数组索引作为参数传递
- 当对象本身存储在 JavaScript 的数组索引中时,如何打印对象的每个单独属性
- 如何使用数组索引序列
- ASP.NET/JavaScript:数组索引溢出:实体框架/LINQ
- 如何在分配给数组索引的对象上调用方法
- 使用铆钉访问数组索引.js
- 计算 Javascript 数组索引的出现次数