我正在尝试在回送3.X中对嵌套模型进行地理空间查询。
基本上我有3种具有以下关系的模型:
类别
"relations": {
"deals": {
"type": "hasAndBelongsToMany",
"model": "Deal",
"foreignKey": ""
}
},
交易
"relations": {
"categories": {
"type": "hasAndBelongsToMany",
"model": "Category",
"foreignKey": ""
},
"locations": {
"type": "hasAndBelongsToMany",
"model": "Location",
"foreignKey": ""
}
}
位置
"relations": {
"deals": {
"type": "hasAndBelongsToMany",
"model": "Deal",
"foreignKey": ""
}
}
我要完成的工作是获取“类别”列表,在其中列出具有某些特征的当前位置,例如地理空间查询。
我无法在嵌套对象中使where过滤器起作用。
我尝试过这样的事情:
Category.near = (center, callback) => {
Category.find({
include: {
relation: 'deals',
scope: {
include: {
relation: 'locations',
where: {
'coordinates': {
geoWithin: {
$centerSphere: [[center.lat, center.lng], Utils.kmToRadians(500)],
},
},
},
},
},
},
}, (err, results) => {
if (err) {
const error = new Error(err.toString());
error.status = 500;
return callback(error);
}
return callback(null, results);
});
}
该查询返回了一些结果,但是并未被我应用于位置关系的where子句过滤,它基本上返回了所有位置。
我需要的是一种只返回至少具有一个位置的交易或至少在响应的位置上返回空数组的方法。
这是带有以下参数的查询的结果:
http://localhost:3000/api/categories/near?center=45.2%2C9.2
结果:
{
"name": "Tecnologia",
"image": "https://cdn4.iconfinder.com/data/icons/future-technology-2/91/Future_Technology_10-512.png",
"id": "5d9c5af49df2bb899f83ef0c",
"deals": [
{
"id": "5d8cb5f53900d859ac6280da",
"title": "Offerta Gianluca",
"description": "Meno 30% su Gianluca",
"img": [
"https://www.mcs4you.it/wp-content/uploads/OffertaSpeciale.png"
],
"amount": 100,
"percentage": 30,
"dateStart": "2019-10-16T12:58:29.048Z",
"dateFinish": "2019-10-18T12:58:29.048Z",
"merchantId": "5d8caf157b205d537b33a1e5",
"created_at": "2019-09-26T12:58:29.048Z",
"locations": [
{
"id": "5d9c559b7d24ed7b5e54315e",
"address": "Via Piacenza 20",
"name": "VIA PIACENZA",
"coordinates": {
"lat": 45.4491223,
"lng": 9.2028192
},
"merchantId": "5d8caf157b205d537b33a1e5",
"created_at": "2019-10-08T09:23:39.244Z"
}
],
"categories": [
{
"name": "Tecnologia",
"image": "https://cdn4.iconfinder.com/data/icons/future-technology-2/91/Future_Technology_10-512.png",
"id": "5d9c5af49df2bb899f83ef0c"
}
],
"companiesIncluded": [
{
"id": "5da6de1890d4d141c406bf26",
"name": "Loud",
"magentoId": 12,
"created_at": "2019-10-16T09:08:40.499Z"
}
],
"companiesExcluded": [
{
"id": "5da6de1f90d4d141c406bf27",
"name": "Bike-room",
"magentoId": 122,
"created_at": "2019-10-16T09:08:47.112Z"
}
]
},
{
"id": "5da9942b15f43d4e014f5b7a",
"title": "Deal test lontano",
"description": "Deal test lontano",
"img": [
"https://www.mcs4you.it/wp-content/uploads/OffertaSpeciale.png"
],
"percentage": 30,
"currency": "eur",
"dateStart": "2019-10-18T10:14:41.697Z",
"dateFinish": "2020-10-18T10:14:41.697Z",
"merchantId": "5d8caf157b205d537b33a1e5",
"created_at": "2019-10-18T10:30:03.843Z",
"locations": [
{
"id": "5da9946e15f43d4e014f5b7c",
"address": "Washington DC",
"name": "WASHINGTON DC",
"coordinates": {
"lat": 38.8935128,
"lng": -77.15466
},
"created_at": "2019-10-18T10:31:10.872Z"
}
],
"categories": [
{
"name": "Tecnologia",
"image": "https://cdn4.iconfinder.com/data/icons/future-technology-2/91/Future_Technology_10-512.png",
"id": "5d9c5af49df2bb899f83ef0c"
}
],
"companiesIncluded": [],
"companiesExcluded": []
}
]
}
如您所见,第二笔交易只有一个位置超出了我通过的范围,但它又返回了。
对我的代码中的错误有任何建议吗?