mongoose-deep-populate
mongoose-deep-populate copied to clipboard
Nested object
I have a schema like this:
new Schema({
waitingList: {
__type__: {
current: {
__type__: [{
date: {
__type__: Date
},
user: {
__type__: ObjectId,
ref: "User"
},
}]
},
history: {
__type__: [{
date: {
__type__: Date
},
user: {
__type__: ObjectId,
ref: "User"
},
}]
}
}
},
}, {
autoIndex: true,
typeKey: "__type__"
});
So, in essence a sample document looks like this:
{
waitingList: {
current: [],
history: [{date: someDate, user: myUser}],
}
}
In that case I want to deep populate user, but if I specify the path as "waitingList.history.user". The plugin crashes with error TypeError: Cannot read property 'paths' of undefined at this position: https://github.com/buunguyen/mongoose-deep-populate/blob/0080a0949fef38bcb9541b267a55810c14c78d4c/lib/plugin.js#L260
For some funny reason I am restricted from declaring sub-schema for those embeded documents. It is possible to make it work in such case?
This is what I get if I log schemaPath at line 261
Mixed {
path: 'waitingList',
instance: 'Mixed',
validators: [],
setters: [],
getters: [],
options: { __type__: { current: [Object], history: [Object] } },
_index: null }
So, at line 285 the assignment schema = schemaPath.schema will cause schema become undefined, causing the aforementioned crash.
What is __type__?
It is the typeKey http://mongoosejs.com/docs/guide.html#typeKey
If you can add a failed test to the bugs section, it would help.
Here you are: https://github.com/kevinresol/mongoose-deep-populate/commit/49bd67c0a27e227ecc3d71fb79052bfeb7db269b
Where can I find out more about this usage of type?
For example, what is the difference between
var ItemSchema = new Schema({
waitingList: {
type: {
current: {
type: [{
date: {type: Date},
user: {type: Number, ref: "User.bug47"},
}]
}
}
}
})
and
var ItemSchema = new Schema({
waitingList: {
current: [{
date: {type: Date},
user: {type: Number, ref: "User.bug47"},
}]
}
})
I am not sure if there are any extra information out there other than the link I posted and the source code. I think mongoose will force treating the the field as a type declaration if the field name matches the typeKey option (which defaults to "type")
AFAIK, there should be no different between the two examples, functionally.
Is this has solution?My code has same problems if key was named "type".
@Yuliang-Lee your problem is probably unrelated to deep-populate You may need to specify typeKey in the schema options.
@kevinresol I try to modify typeKey but deep-populate occurs error