From 5e0c73bec7683fc6529d55fa887bc740380ba71e Mon Sep 17 00:00:00 2001 From: biniam Date: Tue, 12 Feb 2019 11:58:33 -0500 Subject: [PATCH] fix: use correct callback for geo find queries --- lib/dao.js | 110 ++++++++++++++++++------------------ test/basic-querying.test.js | 5 ++ 2 files changed, 60 insertions(+), 55 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index cf7dc7ea..9289d64c 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -1531,6 +1531,7 @@ DataAccessObject.find = function find(query, options, cb) { const near = query && geo.nearFilter(query.where); const supportsGeo = !!connector.buildNearFilter; + let geoQueryObject; if (near) { if (supportsGeo) { @@ -1558,65 +1559,64 @@ DataAccessObject.find = function find(query, options, cb) { queryGeo(ctx.query); }); } - - function queryGeo(query) { - function geoCallbackWithoutNotify(err, data) { - const memory = new Memory(); - const modelName = self.modelName; - - if (err) { - cb(err); - } else if (Array.isArray(data)) { - memory.define({ - properties: self.dataSource.definitions[modelName].properties, - settings: self.dataSource.definitions[modelName].settings, - model: self, - }); - - data.forEach(function(obj) { - memory.create(modelName, obj, options, function() { - // noop - }); - }); - - // FIXME: apply "includes" and other transforms - see allCb below - memory.all(modelName, query, options, cb); - } else { - cb(null, []); - } - } - - function geoCallbackWithNotify(err, data) { - if (err) return cb(err); - - async.map(data, function(item, next) { - const context = { - Model: self, - data: item, - isNewInstance: false, - hookState: hookState, - options: options, - }; - - self.notifyObserversOf('loaded', context, function(err) { - if (err) return next(err); - next(null, context.data); - }); - }, function(err, results) { - if (err) return cb(err); - geoCallbackWithoutNotify(null, results); - }); - } - - const geoCallback = options.notify === false ? geoCallbackWithoutNotify : geoCallbackWithNotify; - invokeConnectorMethod(connector, 'all', self, [{}], options, geoCallback); - } // already handled return cb.promise; } } + function geoCallbackWithoutNotify(err, data) { + const memory = new Memory(); + const modelName = self.modelName; - const allCb = function(err, data) { + if (err) { + cb(err); + } else if (Array.isArray(data)) { + memory.define({ + properties: self.dataSource.definitions[modelName].properties, + settings: self.dataSource.definitions[modelName].settings, + model: self, + }); + + data.forEach(function(obj) { + memory.create(modelName, obj, options, function() { + // noop + }); + }); + + // FIXME: apply "includes" and other transforms - see allCb below + memory.all(modelName, geoQueryObject, options, allCb); + } else { + cb(null, []); + } + } + + function geoCallbackWithNotify(err, data) { + if (err) return cb(err); + + async.map(data, function(item, next) { + const context = { + Model: self, + data: item, + isNewInstance: false, + hookState: hookState, + options: options, + }; + + self.notifyObserversOf('loaded', context, function(err) { + if (err) return next(err); + next(null, context.data); + }); + }, function(err, results) { + if (err) return cb(err); + geoCallbackWithoutNotify(null, results); + }); + } + function queryGeo(query) { + geoQueryObject = query; + const geoCallback = options.notify === false ? geoCallbackWithoutNotify : geoCallbackWithNotify; + invokeConnectorMethod(connector, 'all', self, [{}], options, geoCallback); + } + + function allCb(err, data) { if (!err && Array.isArray(data)) { async.map(data, function(item, next) { const Model = self.lookupModel(item); @@ -1709,7 +1709,7 @@ DataAccessObject.find = function find(query, options, cb) { } else { cb(err, data || []); } - }; + } if (options.notify === false) { invokeConnectorMethod(connector, 'all', self, [query], options, allCb); diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index 57fd3b4e..2610689c 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -654,6 +654,7 @@ describe('basic-querying', function() { if (err) done(err); users.should.have.property('length', 3); users[0].name.should.equal('John Lennon'); + users[0].should.be.instanceOf(User); users[0].addressLoc.should.not.be.null(); done(); }); @@ -677,6 +678,7 @@ describe('basic-querying', function() { if (err) done(err); users.should.have.property('length', 2); users[0].name.should.equal('John Lennon'); + users[0].should.be.instanceOf(User); users[0].addressLoc.should.not.be.null(); users[0].vip.should.be.true(); done(); @@ -708,6 +710,7 @@ describe('basic-querying', function() { if (err) done(err); users.should.have.property('length', 1); users[0].name.should.equal('John Lennon'); + users[0].should.be.instanceOf(User); users[0].addressLoc.should.not.be.null(); users[0].vip.should.be.true(); users[0].order.should.equal(2); @@ -738,6 +741,7 @@ describe('basic-querying', function() { users.should.have.property('length', 2); users[0].addressLoc.should.not.be.null(); users[0].name.should.equal('Paul McCartney'); + users[0].should.be.instanceOf(User); users[1].addressLoc.should.not.equal(null); users[1].name.should.equal('John Lennon'); done(); @@ -775,6 +779,7 @@ describe('basic-querying', function() { users.should.have.property('length', 1); users[0].addressLoc.should.not.be.null(); users[0].name.should.equal('John Lennon'); + users[0].should.be.instanceOf(User); users[0].vip.should.be.true(); done(); });