-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
The below script works as expected, getters don't run on the first 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:
Please modify the above script to repro your issue. |
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:
|
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
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
The text was updated successfully, but these errors were encountered: