mblarsen / mongoose-hidden

A Mongoose schema plugin for filtering properties you usually do not want to sent client-side like passwords and IDs.
MIT License
89 stars 19 forks source link

Ghost properties appearing when using subdocuments #3

Closed numbnut closed 8 years ago

numbnut commented 8 years ago

I'm experiencing a strange behavior when I'm using subdocuments and and mongoose hidden. I prepared a small code snippet which can be run in the test suite.

describe("Test", function () {
    it("Test", function (done) {
      var User = nextModel({
        name: String,
        email: {
          prefix: String,
          suffix: String
        },
        password: String
      });
      var testUser = {name: "Joe", email: {prefix: 'bla', suffix: 'moep.com'}, password: "secret"}
      var user = new User(testUser);
      var userJson = user.toObject();
      console.log(userJson)
      done();
    });
  });

The output of the console.log statement is as follows.

{ name: 'Joe',
  'email.prefix': undefined,
  'email.suffix': undefined,
  password: 'secret',
  email: { prefix: 'bla', suffix: 'moep.com' } }

As you can see there are two "ghost properties" email.prefix and email.suffix.

Does the statement in the readme "Recursive use of hide not supported." cover this case? Thank you.

mblarsen commented 8 years ago

No this doesn't fall under "Recursive use of hide not supported". I'll examine the problem.

mblarsen commented 8 years ago

Commit 3e023fd solves your problem. I've added your test-case to the test-suite. Thanks @numbnut

numbnut commented 8 years ago

Thank you for the quick fix and response!

mblarsen commented 8 years ago

It has been pushed to NPM as well as v0.6.1