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

Getters are called even though the getters flag is not set in the toObject function #7442

Closed
ajith92 opened this issue Jan 23, 2019 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ajith92
Copy link

ajith92 commented Jan 23, 2019

I am reporting a Bug, where getters are called even though getters flag is not set in the toObject function. when toObject is called with the getters flag as true, like below. getters function gets called twice.

document.toObject({getters: true})

This behavior has started to appear from version greater than 5.3.4.

My node.js, mongoose and MongoDB version.
node: v10.14.0
mongoose: 5.4.5
mongodb: v4.0.0

@ajith92 ajith92 changed the title Getters are called even though the getters flags are not set in the toObject function Getters are called even though the getters flag is not set in the toObject function Jan 23, 2019
@vkarpov15
Copy link
Collaborator

The below script works as expected, getters don't run on the first toObject() and getters don't get double called.

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `gh7442`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString, { useNewUrlParser: true });
  await mongoose.connection.dropDatabase();

  const schema = new Schema({
    name: {
      type: String,
      get: function(v) {
        console.log('Getter called');
        return v;
      }
    }
  });
  const Model = mongoose.model('gh7442', schema);

  const doc = new Model({ name: 'test' });

  console.log('-------');

  doc.toObject();

  console.log('-------');

  doc.toObject({ getters: true });
}

Output:

$ node gh-7442.js 
-------
-------
Getter called
done
^C
$ 

Please modify the above script to repro your issue.

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Jan 25, 2019
@ajith92
Copy link
Author

ajith92 commented Jan 28, 2019

The issue occurs for the getters of child schema. Code for the same is below

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `gh7442`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString, { useNewUrlParser: true });
  await mongoose.connection.dropDatabase();

  const childSchema = new Schema({
    value: {
      type: String,
      get: function(v) {
        console.log('Getters called')
        return v
      }
    },
    status: {
      type: String
    }
  }, {
    _id: 1
  })

  const schema = new Schema({
    name: {
      type: childSchema,
    }
  });
  const Model = mongoose.model('gh7442', schema);

  const doc = new Model({ 'name.value': 'test' });

  console.log('-------');

  doc.toObject();

  console.log('-------');

  doc.toObject({ getters: true });
}

Output:

Getters called
-------
-------
Getters called
Getters called
done

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jan 31, 2019
@vkarpov15 vkarpov15 added this to the 5.4.9 milestone Jan 31, 2019
vkarpov15 added a commit that referenced this issue Jan 31, 2019
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