我有两个集合“内容”和“单元”。在内容集合中有一个字段“unitID”,它指的是单元集合。在 meteor 发布功能中,我想添加所有新创建内容的单位类型名称:
Meteor.publish("contents", function () {
var self = this;
var handle = Contents.find().observe({
changed: function(contentdoc, contentid) {
var UnitName = Units.findOne({_id: contentdoc.unittypeid }, {fields: {type: 1}});
self.set("contents", contentid, {'content.0.typename': UnitName});
self.flush();
}
});
}
这有效,但它创建了一个新属性“content.0.UnitName”,而不是在内容数组的第一个元素中插入属性“UnitName”:
[
{
_id:"50bba3ca8f3d1db27f000021",
'content.0.UnitName':
{
_id:"509ff643f3a6690c9ca5ee59",
type:"Drawer small"
},
content:
[
{
unitID:"509ff643f3a6690c9ca5ee59",
name: 'Content1'
}
]
}
]
我想要的是:
[
{
_id:"50bba3ca8f3d1db27f000021",
content:
[
{
unitID:"509ff643f3a6690c9ca5ee59",
name: 'Content1',
UnitName:
{
_id:"509ff643f3a6690c9ca5ee59",
type:"Drawer small"
}
}
]
}
]
我做错了什么?
最佳答案
Meteor.publish
中的
this.set
仅适用于对象的顶级属性,这意味着它不支持 Mongo 风格的点状属性。您必须使用 contents
数组的全新值调用 set
。
关于arrays - 如何在 meteor 发布函数中设置嵌套数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13771234/