gpt4 book ai didi

arrays - 如何在 meteor 发布函数中设置嵌套数组值?

转载 作者:可可西里 更新时间:2023-11-01 09:57:08 26 4
gpt4 key购买 nike

我有两个集合“内容”和“单元”。在内容集合中有一个字段“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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com