0

I've encountered a weird situation, I've been using the extend library and the _.extend command from underscore, in the following way:

var extended = extend({}, objA, objB);

I get only properties from one of the objects in the result, is there anything about extend that might be preventing it from getting all the properties from both objects?

I've used this pattern everywhere and it seemed to work as expected so far. Both are plain objects, not arrays or functions.

EDIT: I tried to make a demo, but it seems to work with both jQuery and Underscore - http://jsfiddle.net/x4UHQ/2/

While making the demo (from the objects I printed in my logs), I noticed one that when printed in console.log didn't appear as a string - it's a mongoose type of ObjectId, maybe that's somehow causing the problem? (that specific field exists in the output object, so I don't know)

UPDATE: this was the solution - UnderscoreJS wont extend object (add a lean option to the mongoose query - because that's somehow makes the object incompatible with underscore.

Community
  • 1
  • 1
Madd0g
  • 3,841
  • 5
  • 37
  • 59
  • put the contents of objA and objB in the question – Mukesh Soni Mar 14 '14 at 18:09
  • I'll try to build something that replicates the problem, I just feel so ridiculous doing it because it's the last place I expected my code to break, on a utility command I use all the time – Madd0g Mar 14 '14 at 18:12
  • 3
    underscore library is tested to death and used in large websites. and extend is one of the most popular functions. So i would bet against a bug in extend. – Mukesh Soni Mar 14 '14 at 18:13
  • Are you by chance looking for [recursive extension or deep merging](http://stackoverflow.com/questions/14843815/recursive-extend-in-underscore-js)? `_.extend()` only performs a *shallow* merge, or just the own properties of `objA` and `objB`. It doesn't merge the objects they each may hold. – Jonathan Lonowski Mar 14 '14 at 18:17
  • Is it possible objA has a property name same as in objB? In that case one overwrites the other. – palanik Mar 14 '14 at 18:18
  • I built the fiddle - http://jsfiddle.net/x4UHQ/2/ with the exact same properties and types, it seems to work. Why wouldn't it work in node? – Madd0g Mar 14 '14 at 18:25
  • please see edit with more information and demo – Madd0g Mar 14 '14 at 18:31
  • please see [this question](http://stackoverflow.com/questions/9418967/underscores-cloning-of-mongoose-objects-and-deleting-properties-not-working) - could that be what's happening here? – Madd0g Mar 14 '14 at 18:34
  • wow, solved, unbelievable. it was indeed an issue with mongoose. – Madd0g Mar 14 '14 at 19:01
  • @Madd0g Please answer the question yourself and accept it in two days, so that the future readers will know how to fix it and the question will also be complete. – thefourtheye Mar 15 '14 at 06:56

2 Answers2

1

Sorry for new response, it better fits as comments for Madd0g response (but i don't have required reputation points for it).

You are right, it is fault of mongoose object, {lean: true} is one option to fix it, other options is calling mongooseObj.field.toJSON() - in some case it is more convenient method. For example if you have user object in controller (from auth middleware), and you need to transform some fields, then you don't need to refetch model from db.

m2gikbb
  • 61
  • 2
0

So the problem was that one of the objects was an object from the mongoose library, it made extend commands break, the fix was to get the db object from mongoose with the lean option.

something like personModel.find({fname: "Joe"}, null, { lean: true })

Madd0g
  • 3,841
  • 5
  • 37
  • 59