查询uni-app地理位置操作符 geoWithin 报错
查询uni-app地理位置操作符 geoWithin 报错 产品分类:
- uniCloud/支付宝小程序云
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
微信小程序 | - | - |
示例代码:
async queryRestaurentsByPolygon() {
try {
const area = new db.Geo.Polygon(
[
new db.Geo.LineString([
new db.Geo.Point(112.82216727560444, 28.134772708582847),
new db.Geo.Point(113.00070445485858, 28.134772708582847),
new db.Geo.Point(113.00070445485858, 28.46126612494616),
new db.Geo.Point(112.82216727560444, 28.46126612494616),
new db.Geo.Point(112.82216727560444, 28.134772708582847),
])
]
)
console.log(JSON.stringify(area))
const res = await db.collection('restaurants_baseinfo').where({
location: db.command.geoWithin({
geometry: area
})
}).get()
console.log(res)
return {
code: 0,
msg: '获取成功',
data: res.data,
}
} catch (error) {
//TODO handle the exception
console.log(error)
return {
errCode: 'DATABASE_ERROR',
errMsg: '数据库操作失败',
}
}
}
修改为
const area = new db.Geo.Polygon([
[
new db.Geo.Point(112.82216727560444, 28.134772708582847),
new db.Geo.Point(113.00070445485858, 28.134772708582847),
new db.Geo.Point(113.00070445485858, 28.46126612494616),
new db.Geo.Point(112.82216727560444, 28.46126612494616),
new db.Geo.Point(112.82216727560444, 28.134772708582847),
],
])
后报错信息为Error: ==右侧值类型不正确
---
### 操作步骤:
在微信小程序中调用云对象中使用 geoWithin 操作符的方法时报错
预期结果:
能成功执行查询语句并返回数据
---
### 实际结果:
FaasError: invalid $geoWithin
bug描述:
在云对象中使用 db.command.geoWithin 方法 报错。 本地调试 和 云端调试 的日志输出报错信息均为:FaasError: invalid $geoWithin 。 前端调用平台: 微信小程序。 地理位置索引也配置了。 表字段,测试数据相关信息如图所示。
更多关于查询uni-app地理位置操作符 geoWithin 报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
再记录一下:
换成阿里云空间之后 查询语句可以正常运行了。
下图是jql测试运行结果。
更多关于查询uni-app地理位置操作符 geoWithin 报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
根据提供的代码和报错信息,问题可能出在geoWithin查询的格式上。在uniCloud中,geoWithin操作符的正确使用方式如下:
-
首先确保数据库的location字段已建立地理位置索引
-
修改geoWithin查询格式为:
const res = await db.collection('restaurants_baseinfo').where({
location: db.command.geoWithin({
geometry: area,
// 或者使用polygon格式
polygon: [
[112.822167, 28.134772],
[113.000704, 28.134772],
[113.000704, 28.461266],
[112.822167, 28.461266]
]
})
}).get()
- 检查area对象的格式是否正确,可以尝试直接使用坐标数组而不是Geo对象:
const polygon = [
[112.822167, 28.134772],
[113.000704, 28.134772],
[113.000704, 28.461266],
[112.822167, 28.461266]
]