You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * BUG REPORT: `toObject()` transform causes lost data during save. */constmongoose=require('mongoose');/** Embedded Schema */constMyEmbeddedSchema=mongoose.Schema({someMoreData: String,},);/** * This plugin filters out the `someMoreData` property * when calling `toObject()` or `toJSON()` on MyEmbeddedSchema. */MyEmbeddedSchema.plugin(schema=>{constfilterSomeMoreData=(doc,returnDoc)=>{deletereturnDoc.someMoreData;returnreturnDoc;};// The use case for this is hiding sensitive data from the clientschema.options.toJSON={transform: filterSomeMoreData};schema.options.toObject={transform: filterSomeMoreData};});/** Base Schema */constMySchema=mongoose.Schema({embedded: MyEmbeddedSchema,});constModel=mongoose.model('MODEL',MySchema);/** Application Logic */(async()=>{awaitnewPromise(resolve=>{mongoose.Promise=global.Promise;mongoose.connect('mongodb://127.0.0.1/mytestdb',{useMongoClient: true});mongoose.connection.once('open',()=>{resolve(mongoose.connection);});});constmodel=newModel();// Commenting this out results in `someMoreData` saving as expectedawaitmodel.save();model.embedded={someMoreData: 'something',};awaitmodel.save();constfetched=awaitModel.findById(model._id).exec();process.stdout.write(`Should log 'something': ${fetched.embedded.someMoreData}`);awaitmongoose.connection.close();})();
The output of this script is:
$ node BUG.js
Should log `something`: undefined
The text was updated successfully, but these errors were encountered:
The output of this script is:
$ node BUG.js Should log `something`: undefined
The text was updated successfully, but these errors were encountered: