UnderscoreJS 不会扩展对象 [英] UnderscoreJS wont extend object
问题描述
我正在尝试使用下划线合并 2 个对象.目标对象是一个猫鼬模型,但我已经对其应用了 lean()
以使其返回一个 javascript 对象而不是一个 mongo 文档.
I am trying to merge 2 objects using underscore. The destination object is a mongoose model but I have applied lean()
to it to make it return a javascript object rather than a mongo document.
model.find({}).lean().exec(function (error, object) {});
然后我尝试使用下划线进行扩展
I then try extending using underscore
_.extend(object, source);
但它只返回源对象.我尝试过使用简单的对象进行测试,并且效果很好,所以我假设它与猫鼬有关?
But it only returns the source object. I have tried with simple objects to test and those worked fine, so I am assuming it has something to do with mongoose?
工作的简单对象是:
{foo:'foo'},{bar:'bar'}
我尝试合并但无法合并的对象是:
And the objects that I am trying to merge but haven't been able to are:
{
_id: 526540eaa77883d815000029,
name: 'House',
description: '',
type: 'residential',
cost: 100,
buildTime: 5,
resources: { produces: [], required: { wood: 5 } },
population: { provides: 10, required: 0 },
requires: [],
maxLevel: 5,
upgrades:
{ '2': { resourceMultiplier: 1.2, cost: 150, time: 5 },
'3': { resourceMultiplier: 1.5, cost: 200, time: 7 },
'4': { resourceMultiplier: 2, cost: 300, time: 10 },
'5': { resourceMultiplier: 2.5, cost: 500, time: 15 } },
scale: { x: 1, y: 1, z: 1 }
}
{
empireId: '52654578a4eff60000000001',
buildingId: '526540eaa77883d815000029',
level: 1,
isComplete: false,
isUpgrading: false,
gridId: '175|0|125',
started: 1382442513823,
_id: 526666113fccae68be000003,
__v: 0
}
有没有人遇到过这个问题或者知道我哪里出错了?
Anyone come across this before or know where I am going wrong?
推荐答案
好吧,我很笨.源对象是从文件顶部的另一个 mongoose 查询中获取的,这个是 mongoose.Document 的一个实例,因此无法更改.我向其中添加了 lean()
以使其返回一个 javascript 对象,现在一切正常.
Well I am stupid. The source object was gotten from another mongoose query at the top of the file, this one was an instance of mongoose.Document and therefore couldn't be changed. I added lean()
to it to make it return a javascript object and it's all working now.
这篇关于UnderscoreJS 不会扩展对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!