使用NodeJS,BookshelfJS和Knex进行PostGIS查询

问题描述:

我在使用NodeJS,BookshelfJS和ExpressJS进行项目。 我的数据库是安装了Postgis的Postgres。 我的表'组织'有一个'lat_lon'几何列。 我想查询特定经/纬度点的固定半径内的所有组织。 我想是这样的:使用NodeJS,BookshelfJS和Knex进行PostGIS查询

var organizations = await Organization.query(function (qb) { 
qb.where('ST_DWithin(lat_lon, ST_GeomFromText("POINT(45.43 10.99)", 4326), 1000)') 
}).fetchAll() 

和更多的组合,但它不工作。 它返回我一个错误

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: The operator "undefined" is not permitted

看来它期待运营商内部的where条件,但我已经工作的“lat_lon”列。

我该如何解决? 谢谢

您是否尝试过使用knex.raw()?

var organizations = await Organization.query(function (qb) { 
qb.where(knex.raw('ST_DWithin(lat_lon, ST_GeomFromText("POINT(45.43 10.99)", 4326), 1000)')) 
}).fetchAll() 
+0

它不起作用。我有这个错误:“UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:2):错误:从”组织“中选择”机构“*其中ST_DWithin(lat_lon,ST_GeomFromText(”POINT(45.43 10.99)“,4326) - 列“POINT(45.43 10.99)”不存在“。 我的专栏称为“lat_lon”,是一个几何图形。 – Matteo

+0

它的工作原理如果我这样做:'ST_DWithin(lat_lon,ST_MakePoint(45.43,10.99),1000)'。有什么不同? – Matteo