Skip to content

Commit

Permalink
Merge PR for removing methods from toJSON. Closes #79
Browse files Browse the repository at this point in the history
  • Loading branch information
scottwrobinson committed Jun 27, 2016
2 parents d23c6d9 + 4cc75a1 commit 997e6de
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
9 changes: 1 addition & 8 deletions lib/base-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,7 @@ class BaseDocument {
}
}
}
var proto = Object.getPrototypeOf(this);
var protoProps = Object.getOwnPropertyNames(proto);
for (var i = 0; i < protoProps.length; i++) {
key = protoProps[i];
if (key !== 'constructor' && key !== 'id'){
values[key] = this[key];
}
}

return values;
}

Expand Down
26 changes: 26 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1649,5 +1649,31 @@ describe('Document', function() {
expect(json.children[1]).to.not.be.an.instanceof(Person);
}).then(done, done);
});

it('should serialize data to JSON and ignore methods', function(done) {
class Person extends Document {
constructor() {
super();

this.name = String;
}

static collectionName() {
return 'people';
}

getFoo() {
return 'foo';
}
}

var person = Person.create({
name: 'Scott'
});

var json = person.toJSON();
expect(json).to.have.keys(['_id', 'name']);
done();
});
});
});
43 changes: 43 additions & 0 deletions test/embedded.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,5 +669,48 @@ describe('Embedded', function() {
expect(json.address.isPoBox).to.be.equal(false);
}).then(done, done);
});

it('should serialize data to JSON and ignore methods', function(done) {
class Address extends EmbeddedDocument {
constructor() {
super();
this.street = String;
}

getBar() {
return 'bar';
}
}

class Person extends Document {
constructor() {
super();

this.name = String;
this.address = Address;
}

static collectionName() {
return 'people';
}

getFoo() {
return 'foo';
}
}

var person = Person.create({
name: 'Scott',
address : {
street : 'Bar street'
}
});

var json = person.toJSON();
expect(json).to.have.keys(['_id', 'name', 'address']);
expect(json.address).to.have.keys(['street']);

done();
});
});
});

0 comments on commit 997e6de

Please sign in to comment.