Skip to content

toObject minimization not working with getters #1607

@lbeschastny

Description

@lbeschastny

I played i bit with toObject method and found, that minimize: true flag is being ignored when getters: true is set:

{Schema} = mongoose = require 'mongoose'
ObjectId = Schema.ObjectId

Element = mongoose.model 'Element', new Schema
  field1: String
  field2: String
  nested: field: String

doc = new Element field1: 'foo'

console.log 'nice', doc.toObject minimize: yes
console.log 'ugly', doc.toObject minimize: yes, getters: yes
nice { field1: 'foo', _id: 51fa92d61061e9ce7e000001 }
ugly { field1: 'foo',
  _id: 51fa92d61061e9ce7e000001,
  id: '51fa92d61061e9ce7e000001',
  __v: undefined,
  nested: { field: undefined },
  field2: undefined }

It's not a problem for top-level fields, because JSON serialization removes such fields. But nested field becomes an empty object, which is not what I want to send to the client:

{ field1: 'foo',
  _id: '51fa9436bd6afbc707000001',
  id: '51fa9436bd6afbc707000001',
  nested: {} }

It's not only about the amount of data I'm sending. For most client side frameworks {} means that my document has a nested sub-document, which is not true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions