Skip to content

Virtual fields toJSON does not work #5473

@akaNightmare

Description

@akaNightmare

Do you want to request a feature or report a bug?
Bug.

What is the current behavior?

const schema =  new mongoose.Schema({
   some_field: Number,
}, {
    toObject: { virtuals: true },
    toJSON: { virtuals: true },
})
schema.virtual('test.a', function() {
    return 1;
});
schema.virtual('test.b', function() {
    return 2;
})

///
console.log(model.toJSON()); // don't see test object
{
    some_field: 1
}

What is the expected behavior?

{
    some_field: 1,
    test: {a:1,b:1}
}

but when model.test.a -> works, toJSON -> not
Please mention your node.js, mongoose and MongoDB version.
Mongoose: 4.11.3
NodeJS: 7.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions