سؤال

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.

هل كانت مفيدة؟

المحلول 2

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 })

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top