0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.