I am using Mongoose and I want to remove the _id property from my Mongoose instance before sending a JSON response to the client.
Example:
var ui = _.clone(userInvite); delete ui["_id"]; console.log(JSON.stringify(ui));
The previous one did not work.
However, if I do this:
var ui = JSON.parse(JSON.stringify(userInvite)); //poor man clone delete ui["_id"]; console.log(JSON.stringify(ui)); //"_id" is gone! it works!
I do not understand why calling delete for a cloned object using Underscore does not work, but if I execute the hacker JSON.string / JSON.parse, it works.
Any thoughts on this behavior?
JP Richardson
source share