mongoose: Sub document refs undefined, when not populating in 4.0.2
I am having a strange issue
var UserSchema = new Schema({
application: { type: Schema.Types.ObjectId, ref: 'Application', required: true, index: true },
name: { type: String, required: true, index: true },
});
var User = mongoose.model('user', UserSchema)
User.findOne{ {_id: <id> }).exec(function(err, user) {
console.log(user);
// { _id: <id>, name: <name> } <-- application is missing even when explicitly set in db console
});
Any properties that are refs never get added to the document, if I remove the ‘ref’ statement, the objects load just fine. I am NOT trying to populate the sub-document in this instance, I just want the sub-document id.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 33 (1 by maintainers)
Commits related to this issue
- fix(schema): correctly handle ref ObjectIds from different mongoose libs Fix #5259, Re: #2951 — committed to Automattic/mongoose by vkarpov15 7 years ago
Okay, so just as @MichielDeMey mentioned the issue is apparently caused by the schema being created in a separate node module (using a separate mongoose instance).
I don’t know if this is not allowed or if it’s a bug, but either way I have created a repo reproducing the issue: https://github.com/whats0n0/mongoose-2951