fix: use correct callback for geo find queries

This commit is contained in:
biniam 2019-02-12 11:58:33 -05:00
parent c7d23d18d0
commit 5e0c73bec7
2 changed files with 60 additions and 55 deletions

View File

@ -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);

View File

@ -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();
});