Underscore's Cloning of Mongoose Objects and Deleting Properties Not Working? Underscore's Cloning of Mongoose Objects and Deleting Properties Not Working? mongoose mongoose

Underscore's Cloning of Mongoose Objects and Deleting Properties Not Working?


I just came across a similar issue trying to replace _id with id. Doing this worked for me:

Schema.methods.toJSON = function(options) {  var document = this.toObject(options);  document.id = document._id.toHexString();  delete(document._id);  return document;};

Maybe it will start working if you replace delete ui["_id"] with delete ui._id or use toObject instead of _.clone.


Just to add to the previous answer, there is one more way to achieve the same. 'toObject' function applies transformation to the document which is defined by the schema.options.toObject.transform function, e.g

schema.options.toObject.transform = function(doc, ret) {    ret.id = doc._id;    delete ret._id;};