Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Virtuals not rendering on toObject({virtuals : true}); #889

Closed
guyht opened this Issue · 4 comments

2 participants

@guyht

The following code:

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test');

var A = new mongoose.Schema({
    b : [B]
});

var B = new mongoose.Schema({});

A.virtual('v1').get(function() {
 console.log('Virtual v1');
 return 'v1';
});

B.virtual('v2').get(function() {
 console.log('Virtual v2');
 return 'v2';
});


var a = mongoose.model('A', A);
var b = mongoose.model('B', B);

var m = new a({b : new b});

m.save(function(err) {
 if(err) console.log(err);
});

m = m.toObject({getters : true, virtuals : true});

console.log(JSON.stringify(m));

Only renders the first virtual, event though both virtual functions are invoked:

Virtual v2
Virtual v1
{"_id":"4fa1616cb7f24d577e000002","b":[{"_id":"4fa1616cb7f24d577e000001"}],"v1":"v1","id":"4fa1616cb7f24d577e000002"}
@aheckmann
Owner

always declare the nested schemas before passing them into their parent.

var B = new mongoose.Schema({});
var A = new mongoose.Schema({
    b : [B]
});
@aheckmann aheckmann closed this
@guyht

@aheckmann The result is identical. Please reopen.

@aheckmann aheckmann reopened this
@aheckmann
Owner

@guyht thanks for being persistent. got it fixed in the above PR

@aheckmann aheckmann closed this in 98f8627
@guyht

Great, thanks.

@aheckmann aheckmann referenced this issue from a commit in aheckmann/mongoose
@aheckmann aheckmann fix sub doc toObject virtuals application
closes #889
512d27f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.