diff --git a/lib/model.js b/lib/model.js index 8547d727..766f7d45 100644 --- a/lib/model.js +++ b/lib/model.js @@ -226,7 +226,7 @@ ModelBaseClass.prototype.toObject = function (onlySchema) { if (schemaLess) { Object.keys(self.__data).forEach(function (propertyName) { if (!data.hasOwnProperty(propertyName)) { - var val = self.__data[propertyName]; + var val = self.hasOwnProperty(propertyName) ? self[propertyName] : self.__data[propertyName]; if(val !== undefined && val!== null && val.toObject) { data[propertyName] = val.toObject(!schemaLess); } else { @@ -290,4 +290,4 @@ ModelBaseClass.mixin = function(anotherClass, options) { }; jutil.mixin(ModelBaseClass, Hookable); -jutil.mixin(ModelBaseClass, validations.Validatable); \ No newline at end of file +jutil.mixin(ModelBaseClass, validations.Validatable); diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index c2c4fdca..e095a0cb 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -369,6 +369,21 @@ describe('DataSource define model', function () { done(null, User); }); + + it('should change the property value for save if strict=false', function (done) { + var ds = new DataSource('memory');// define models + var Post = ds.define('Post'); + + Post.create({price: 900}, function(err, post) { + assert.equal(post.price, 900); + post.price = 1000; + post.save(function(err, result) { + assert.equal(1000, result.price); + done(err, result); + }); + }); + }); + });