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

fix(virtualtype): order in applyGetters and applySetters #8897

Merged
merged 3 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/virtualtype.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ VirtualType.prototype.set = function(fn) {

VirtualType.prototype.applyGetters = function(value, doc) {
let v = value;
for (let l = this.getters.length - 1; l >= 0; l--) {
v = this.getters[l].call(doc, v, this, doc);
for (const getter of this.getters) {
v = getter.call(doc, v, this, doc);
}
return v;
};
Expand All @@ -155,8 +155,8 @@ VirtualType.prototype.applyGetters = function(value, doc) {

VirtualType.prototype.applySetters = function(value, doc) {
let v = value;
for (let l = this.setters.length - 1; l >= 0; l--) {
v = this.setters[l].call(doc, v, this, doc);
for (const setter of this.setters) {
v = setter.call(doc, v, this, doc);
}
return v;
};
Expand Down
14 changes: 10 additions & 4 deletions test/docs/schemas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ describe('Advanced Schemas', function () {
it('Creating from ES6 Classes Using `loadClass()`', function(done) {
const schema = new Schema({ firstName: String, lastName: String });

class PersonClass {
class HumanClass {
get fullName() {
return 'My name';
}
}

class PersonClass extends HumanClass {
// `fullName` becomes a virtual
get fullName() {
return `${this.firstName} ${this.lastName}`;
return `${super.fullName} is ${this.firstName} ${this.lastName}`;
}

set fullName(v) {
Expand Down Expand Up @@ -56,14 +62,14 @@ describe('Advanced Schemas', function () {

Person.create({ firstName: 'Jon', lastName: 'Snow' }).
then(doc => {
assert.equal(doc.fullName, 'Jon Snow');
assert.equal(doc.fullName, 'My name is Jon Snow');
doc.fullName = 'Jon Stark';
assert.equal(doc.firstName, 'Jon');
assert.equal(doc.lastName, 'Stark');
return Person.findByFullName('Jon Snow');
}).
then(doc => {
assert.equal(doc.fullName, 'Jon Snow');
assert.equal(doc.fullName, 'My name is Jon Snow');
// acquit:ignore:start
done();
// acquit:ignore:end
Expand Down