Fix the coercion issue related to GeoPoint near
This commit is contained in:
parent
dab99c4ebb
commit
fab504b5d9
24
lib/dao.js
24
lib/dao.js
|
@ -396,10 +396,24 @@ DataAccessObject._coerce = function (where) {
|
|||
if (!DataType) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (DataType === geo.GeoPoint) {
|
||||
// Skip the GeoPoint as the near operator breaks the assumption that
|
||||
// an operation has only one property
|
||||
// We should probably fix it based on
|
||||
// http://docs.mongodb.org/manual/reference/operator/query/near/
|
||||
// The other option is to make operators start with $
|
||||
continue;
|
||||
}
|
||||
|
||||
var val = where[p];
|
||||
// Check there is an operator
|
||||
var operator = null;
|
||||
if ('object' === typeof val && Object.keys(val).length === 1) {
|
||||
if ('object' === typeof val) {
|
||||
if (Object.keys(val).length !== 1) {
|
||||
// Skip if there are not only one properties
|
||||
continue;
|
||||
}
|
||||
for (var op in operators) {
|
||||
if (op in val) {
|
||||
val = val[op];
|
||||
|
@ -455,6 +469,11 @@ DataAccessObject.find = function find(params, cb) {
|
|||
var constr = this;
|
||||
|
||||
params = params || {};
|
||||
|
||||
if(params.where) {
|
||||
params.where = this._coerce(params.where);
|
||||
}
|
||||
|
||||
var fields = params.fields;
|
||||
var near = params && geo.nearFilter(params.where);
|
||||
var supportsGeo = !!this.getDataSource().connector.buildNearFilter;
|
||||
|
@ -465,9 +484,6 @@ DataAccessObject.find = function find(params, cb) {
|
|||
}
|
||||
|
||||
params = removeUndefined(params);
|
||||
if(params.where) {
|
||||
params.where = this._coerce(params.where);
|
||||
}
|
||||
if(near) {
|
||||
if(supportsGeo) {
|
||||
// convert it
|
||||
|
|
|
@ -106,8 +106,8 @@ function GeoPoint(data) {
|
|||
}
|
||||
|
||||
assert(typeof data === 'object', 'must provide a lat and lng object when creating a GeoPoint');
|
||||
assert(typeof data.lat === 'number', 'lat must be a number when creating a GeoPoint');
|
||||
assert(typeof data.lng === 'number', 'lng must be a number when creating a GeoPoint');
|
||||
assert(typeof data.lat === 'number' && !isNaN(data.lat), 'lat must be a number when creating a GeoPoint');
|
||||
assert(typeof data.lng === 'number' && !isNaN(data.lng), 'lng must be a number when creating a GeoPoint');
|
||||
assert(data.lng <= 180, 'lng must be <= 180');
|
||||
assert(data.lng >= -180, 'lng must be >= -180');
|
||||
assert(data.lat <= 90, 'lat must be <= 90');
|
||||
|
|
|
@ -582,6 +582,7 @@ describe('DataAccessObject', function () {
|
|||
age: Number,
|
||||
vip: Boolean,
|
||||
date: Date,
|
||||
location: 'GeoPoint',
|
||||
scores: [Number]
|
||||
});
|
||||
});
|
||||
|
@ -647,6 +648,12 @@ describe('DataAccessObject', function () {
|
|||
assert.deepEqual(where, {vip: false});
|
||||
|
||||
});
|
||||
|
||||
it('should skip GeoPoint', function () {
|
||||
where = model._coerce({location: {near: {lng: 10, lat: 20}, maxDistance: 20}});
|
||||
assert.deepEqual(where, {location: {near: {lng: 10, lat: 20}, maxDistance: 20}});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Load models from json', function () {
|
||||
|
|
Loading…
Reference in New Issue