-
-
Couldn't load subscription status.
- Fork 3.9k
Closed
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Description
Do you want to request a feature or report a bug?
Report a bug.
What is the current behavior?
See comments on two last lines of code - getter is skipped when calling toJSON on parent, despite having "getters: true" on all schemas.
If the current behavior is a bug, please provide the steps to reproduce.
var { model, Schema } = require('mongoose');
var childSchema = new Schema({}, {
toJSON: { getters: true }
});
childSchema.virtual('field')
.get(function(){
return this._field;
})
.set(function(v){
return this._field = v;
});
var Child = model('Child', childSchema);
var parentSchema = new Schema({
child: { type: Schema.Types.ObjectId, ref: 'Child', get: childGetter }
}, {
toJSON: { getters: true }
});
function childGetter(child){
child.field = true;
return child;
}
var Parent = model('Parent', parentSchema);
//=====================
var child = new Child();
var parent = new Parent({ child });
console.log(parent.toJSON().child.field); //prints 'undefined'
console.log(parent.child.toJSON().field); //prints 'true'
What is the expected behavior?
To have "true" as output of both console.logs.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js 8.9.3
Mongoose 5.6.0
MongoDB irrelevant - MWE does not connect to DB at all
Metadata
Metadata
Assignees
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.