From eb6fa410fe68a4ec52384986b1119f6bbf026daf Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 1 Jul 2015 14:58:14 -0700 Subject: [PATCH] Fix coercion from ObjectID to String --- lib/dao.js | 21 ++++++++------------- test/loopback-dl.test.js | 12 ++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 52a82641..02138c3e 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -294,9 +294,9 @@ DataAccessObject.create = function (data, options, cb) { }; Model.notifyObserversOf('loaded', context, function(err) { - // By default, the instance passed to create callback is NOT updated - // with the changes made through persist/loaded hooks. To preserve - // backwards compatibility, we introduced a new setting updateOnLoad, + // By default, the instance passed to create callback is NOT updated + // with the changes made through persist/loaded hooks. To preserve + // backwards compatibility, we introduced a new setting updateOnLoad, // which if set, will apply these changes to the model instance too. if(Model.settings.updateOnLoad) { obj.setAttributes(context.data); @@ -1135,12 +1135,7 @@ DataAccessObject._coerce = function (where) { } // Check there is an operator var operator = null; - if ('object' === typeof val) { - if (Object.keys(val).length !== 1) { - // Skip if there are not only one properties - // as the assumption for operators is not true here - continue; - } + if (val.constructor === Object) { for (var op in operators) { if (op in val) { val = val[op]; @@ -1182,7 +1177,7 @@ DataAccessObject._coerce = function (where) { } } } else { - if (val !== null && val !== undefined) { + if (val != null) { val = DataType(val); } } @@ -2361,9 +2356,9 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op Model.notifyObserversOf('loaded', context, function(err) { if (!err) inst.__persisted = true; - // By default, the instance passed to updateAttributes callback is NOT updated - // with the changes made through persist/loaded hooks. To preserve - // backwards compatibility, we introduced a new setting updateOnLoad, + // By default, the instance passed to updateAttributes callback is NOT updated + // with the changes made through persist/loaded hooks. To preserve + // backwards compatibility, we introduced a new setting updateOnLoad, // which if set, will apply these changes to the model instance too. if(Model.settings.updateOnLoad) { inst.setAttributes(context.data); diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index d2eddd82..b5d0c658 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -1269,6 +1269,18 @@ describe('DataAccessObject', function () { assert.deepEqual(where, {id: '1'}); where = model._coerce({id: '1'}); assert.deepEqual(where, {id: '1'}); + + // Mockup MongoDB ObjectID + function ObjectID(id) { + this.id = id; + } + + ObjectID.prototype.toString = function() { + return this.id; + }; + + where = model._coerce({id: new ObjectID('1')}); + assert.deepEqual(where, {id: '1'}); }); it('should be able to coerce where clause for number types', function () {