mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Schema.path('path to object') returns undefined

Open valterkraemer opened this issue 7 years ago • 1 comments

Do you want to request a feature or report a bug?

bug

What is the current behavior?

MySchema.path('path to object') returns undefined.

If the current behavior is a bug, please provide the steps to reproduce.

Having:

const UserSchema = new Schema({
  test1: {
    a: String
  },
  test2: {
    type: {
      a: String
    }
  },
  test3: [{
    a: String
  }]
});

Will make MySchema.path('test1') == undefined.

While the other ones work: MySchema.path('test2') != undefined. MySchema.path('test3') != undefined.

What is the expected behavior?

I would expect test1 to work the same way as test2.

Please mention your node.js, mongoose and MongoDB version. Node v11.6.0 Mongoose: 5.4.3 MongoDB: v4.0.4

valterkraemer avatar Jan 10 '19 12:01 valterkraemer

That's because:

const UserSchema = new Schema({
  test2: {
    type: {
      a: String
    }
  }
});

is the same as:

const UserSchema = new Schema({
  test2: mongoose.Mixed
});

See mixed docs.

The fact that nested paths aren't full Mongoose schema types is an unfortunate implementation quirk and not something that we can easily work around for now. It's something that we'll improve on for a future release.

vkarpov15 avatar Jan 14 '19 21:01 vkarpov15