Schema.path('path to object') returns undefined
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
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.