Reputation: 9700
I am trying to do something like this:
Industry.findOne({_id: id}).exec(function(err, industry){
industry.stats = _.extend(industry.stats, stats); //.......(1)
industry.save(function(err) {
// nothing is saved
});
});
The console.log of industry.stats in (1) is
[{ stat_id: 545080c8e4e88b1d5a7a6d1b}{ stat_id: 54526ca6b294096d33ca6b36 }]
This is not working, apparently the industry.stats is not an object array and misses a comma in between the two objects. (am I stating this correctly?)
If I assign industry.stats directly like this
[{stat_id: 545080c8e4e88b1d5a7a6d1b}, {stat_id: 54526ca6b294096d33ca6b36}]
Then it's working. Is there something I need to do to convert (1) to an object array first? I tried lean() and toObject()...etc but I got no luck. Am I missing something?
Upvotes: 0
Views: 237
Reputation: 6898
Lodash extend
will assign own enumerable properties of source object to the destination object. In this case array properties of the source (stats
) are copied to industry.stats
. This will not work for arrays.
You'll have to update the array via array functions (push
, pull
,...) or set the field directly.
Upvotes: 1