我很难过..
我使用另一个在控制台中包含嵌套数组的对象测试了此方法,并获得了预期的结果
db_status_emitter.on("components_found", function (returned_item) {
//console.log(returned_item);
prun_item.type = returned_item.type;
prun_item.components = returned_item.components.slice(0);
switch (prun_item.type) {
case "one-sided-mattress":
prun_item.quantity = prun_item.total_panel_quantity;
var top_panel = underscore.find(prun_item.components, function (element) {
return element.component_category == "top-panel"
});
var border = underscore.find(prun_item.components, function (element) {
return element.component_category == "border"
});
top_panel.quantity = border.quantity = prun_item.quantity;
top_panel.style = "91000000";
border.style = "81000000";
break;
case "two-sided-mattress":
prun_item.quantity = prun_item.total_panel_quantity / 2;
var top_panel = underscore.find(prun_item.components, function (element) {
return element.component_category == "top-panel"
});
var bottom_panel = underscore.find(prun_item.components, function (element) {
return element.component_category == "bottom-panel"
});
var border = underscore.find(prun_item.components, function (element) {
return element.component_category == "border"
});
top_panel.quantity = bottom_panel.quantity = border.quantity = prun_item.quantity;
top_panel.style = "91000000";
bottom_panel.style = "92000000";
border.style = "81000000";
break;
default:
console.error("error with prun Schedule");
break;
}
console.log(prun_item);
});
// 编辑:我应该在这里添加mongoose调用;它与.on()函数在同一范围内......我
fex_model.findOne({_id: prun_item.model}, {components: 1, type: 1, _id: 0}, function (err, returned_item){
if (err) db_status_emitter.emit("error", err)
db_status_emitter.emit("components_found", returned_item);
})
为什么没有任何组件显示我给它们添加的键值对?这是一个输出样本..
{ quilter_name: 'Panel Quilter #3',
schedule_no: 55f32e4470df49a65d03cf6a,
model: '3630',
size: '66',
total_panel_quantity: 72,
type: 'two-sided-mattress',
components:
[ { component_category: 'top-panel', buffer_op: '120' },
{ component_category: 'bottom-panel', buffer_op: '120' },
{ component_category: 'border', buffer_op: '119' } ],
quantity: 36 }
但它应该是:
{ quilter_name: 'Panel Quilter #3',
schedule_no: 55f32e4470df49a65d03cf6a,
model: '3630',
size: '66',
total_panel_quantity: 72,
type: 'two-sided-mattress',
components:
[ { component_category: 'top-panel', buffer_op: '120', quantity: 36, style: "91000000" },
{ component_category: 'bottom-panel', buffer_op: '120', quantity: 36, style: "92000000" },
{ component_category: 'border', buffer_op: '119', quantity: 36, style: "81000000" } ],
quantity: 36 }
但是为什么地球上的相同过程会在控制台上运行???
>underscore.extend(extended_this_object, components_wrapper);
{ foo: 'bar',
components:
[ { a: 'findthis', b: '3', c: '4', d: 'new value' },
{ a: 'notthis', b: '4', c: '5' } ] }
>var foundthis2 = underscore.find(extended_this_object.components, function(element){ return element.a == 'findthis'});
>foundthis2
{ a: 'findthis', b: '3', c: '4', d: 'new value' }
>foundthis2.e = 'a newer value'
'a newer value'
>extended_this_object;
{ foo: 'bar',
components:
[ { a: 'findthis',
b: '3',
c: '4',
d: 'new value',
e: 'a newer value' },
{ a: 'notthis', b: '4', c: '5' } ] }
编辑以下是单个组件的密钥输出。我怀疑它与来自猫鼬的密钥有关...
[ '__parentArray',
'__parent',
'__index',
'$__',
'isNew',
'errors',
'_doc',
'$__original_save',
'save',
'_pres',
'_posts',
'quantity',
'style' ]
注意数量和样式是属性本身,我怀疑,记录的属性位于_doc中。
答案 0 :(得分:0)
起初我认为这是一个有趣的问题,但后来我意识到这没有道理。我尝试在js文件中做各种类似的事情并node
但我无法复制你遇到的事情。
日志消息不是来自console.log
,或者在重新启动服务器后无法复制此问题(V8中出现问题片刻?)。
您是否尝试将日志更改为prun_item.components[0].quantity
?
或者你的原始代码可能比这复杂得多,修改_.find
返回的对象不会修改你console.log
的对象,这是唯一的逻辑解释。