Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toObject() transform causes *lost data* during save. #5807

Closed
heisian opened this issue Nov 11, 2017 · 4 comments
Closed

toObject() transform causes *lost data* during save. #5807

heisian opened this issue Nov 11, 2017 · 4 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@heisian
Copy link

heisian commented Nov 11, 2017

/**
 * BUG REPORT: `toObject()` transform causes lost data during save.
 */

const mongoose = require('mongoose');


/** Embedded Schema */
const MyEmbeddedSchema = mongoose.Schema(
  {
    someMoreData: String,
  },
);


/**
 * This plugin filters out the `someMoreData` property
 * when calling `toObject()` or `toJSON()` on MyEmbeddedSchema.
 */
MyEmbeddedSchema.plugin(schema => {
  const filterSomeMoreData = (doc, returnDoc) => {
    delete returnDoc.someMoreData;
    return returnDoc;
  };
  // The use case for this is hiding sensitive data from the client
  schema.options.toJSON = { transform: filterSomeMoreData };
  schema.options.toObject = { transform: filterSomeMoreData };
});


/** Base Schema */
const MySchema = mongoose.Schema(
  {
    embedded: MyEmbeddedSchema,
  }
);

const Model = mongoose.model('MODEL', MySchema);


/** Application Logic */
(async () => {
  await new Promise(resolve => {
    mongoose.Promise = global.Promise;
    mongoose.connect('mongodb://127.0.0.1/mytestdb', { useMongoClient: true });
    mongoose.connection.once('open', () => {
      resolve(mongoose.connection);
    });
  });

  const model = new Model();

  // Commenting this out results in `someMoreData` saving as expected
  await model.save();

  model.embedded = {
    someMoreData: 'something',
  };

  await model.save();

  const fetched = await Model.findById(model._id).exec();

  process.stdout.write(`Should log 'something': ${fetched.embedded.someMoreData}`);

  await mongoose.connection.close();
})();

The output of this script is:

$ node BUG.js
Should log `something`: undefined
@wlingke
Copy link
Contributor

wlingke commented Nov 11, 2017

This is a legit issue.

@vkarpov15 vkarpov15 added this to the 4.13.3 milestone Nov 15, 2017
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Nov 15, 2017
@vkarpov15
Copy link
Collaborator

Thanks for reporting, will fix ASAP

vkarpov15 added a commit that referenced this issue Nov 15, 2017
@vkarpov15
Copy link
Collaborator

Fixed in master, fix will be in 4.13.3. Thanks again for the detailed repro 👍

@heisian
Copy link
Author

heisian commented Nov 15, 2017

thank you for the incredibly quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants