Merge pull request #422 from strongloop/feature/fix-mongodb-issue-87

Remove undefined properties for create
This commit is contained in:
Miroslav Bajtoš 2015-02-04 19:11:02 +01:00
commit 4889443ed0
3 changed files with 51 additions and 1 deletions

View File

@ -231,7 +231,8 @@ DataAccessObject.create = function (data, callback) {
obj.trigger('save', function (saveDone) {
var _idName = idName(Model);
var modelName = Model.modelName;
this._adapter().create(modelName, this.constructor._forDB(obj.toObject(true)), function (err, id, rev) {
var val = removeUndefined(obj.toObject(true));
this._adapter().create(modelName, this.constructor._forDB(val), function (err, id, rev) {
if (id) {
obj.__data[_idName] = id;
defineReadonlyProp(obj, _idName, id);

View File

@ -1,4 +1,5 @@
require('./datatype.test.js');
require('./basic-querying.test.js');
require('./manipulation.test.js');
require('./hooks.test.js');
require('./relations.test.js');

View File

@ -192,6 +192,29 @@ describe('manipulation', function () {
done();
});
});
it('should preserve properties with "undefined" value', function(done) {
Person.create(
{ name: 'a-name', gender: undefined },
function(err, created) {
if (err) return done(err);
created.toObject().should.have.properties({
id: created.id,
name: 'a-name',
gender: undefined
});
Person.findById(created.id, function(err, found) {
if (err) return done(err);
found.toObject().should.have.properties({
id: created.id,
name: 'a-name',
gender: undefined
});
done();
});
});
});
});
describe('save', function () {
@ -349,6 +372,31 @@ describe('manipulation', function () {
});
});
});
it('should preserve properties with "undefined" value', function(done) {
Person.create(
{ name: 'a-name', gender: undefined },
function(err, instance) {
if (err) return done(err);
instance.toObject().should.have.properties({
id: instance.id,
name: 'a-name',
gender: undefined
});
Person.updateOrCreate(
{ id: instance.id, name: 'updated name' },
function(err, updated) {
if (err) return done(err);
updated.toObject().should.have.properties({
id: instance.id,
name: 'updated name',
gender: undefined
});
done();
});
});
});
});
describe('destroy', function () {