Node.js: How can I clone an object with Underscore and assign new properties to the clone afterwards? Node.js: How can I clone an object with Underscore and assign new properties to the clone afterwards? mongoose mongoose

Node.js: How can I clone an object with Underscore and assign new properties to the clone afterwards?


The issue is that the object returned by save is not a "raw" javascript object. Its wrapped by mongoose and has its methods on there. When you clone, you also clone the mongoose stuff!

When a mongoose object is sent down by res.send, it is serialized, and mongoose only serializes the members of the objects that it is aware of. If you want to add properties, you need to do it on a "raw" javascript object. Like so:

var nu = newuser.toJSON();nu.newbie = 'true';res.json(nu);

also, res.json is just a nice shorthand that will take care of converting to json and setting the content type.