Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual field in child is undefined in toJSON from parent #8295

Closed
DenMesh opened this issue Oct 31, 2019 · 2 comments
Closed

Virtual field in child is undefined in toJSON from parent #8295

DenMesh opened this issue Oct 31, 2019 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@DenMesh
Copy link

DenMesh commented Oct 31, 2019

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

@DenMesh
Copy link
Author

DenMesh commented Nov 1, 2019

Upd: if you access the virtual prior to toJSONing it, everything works fine:

var child = new Child();
var parent = new Parent({ child });

console.log(parent.child.field); //prints 'true'
console.log(parent.toJSON().child.field); //prints 'true'
console.log(parent.child.toJSON().field); //prints 'true'

@vkarpov15 vkarpov15 added this to the 5.7.9 milestone Nov 6, 2019
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Nov 6, 2019
@vkarpov15
Copy link
Collaborator

Confirmed this is a bug, looks related to the work from #7442. I tried a few different ways to fix it, but having some trouble getting the tests for this issue, #7442, and #1376 to pass at the same time. Will try again later 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants