【问题标题】:PostGIS query with NodeJS, BookshelfJS and Knex使用 NodeJS、BookshelfJS 和 Knex 进行 PostGIS 查询
【发布时间】:2018-01-27 07:57:37
【问题描述】:

我正在使用 NodeJS、BookshelfJS 和 ExpressJS 进行项目。 我的数据库是安装了 Postgis 的 Postgres。 我的表“组织”有一个“lat_lon”几何列。 我想查询特定纬度/经度点的固定半径内的所有组织。 我尝试过这样的事情:

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

还有更多组合,但它不起作用。 它返回一个错误

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):TypeError:不允许操作符“未定义”

它似乎在 where 条件内期待运算符,但我已经在处理“lat_lon”列。

我该如何解决? 谢谢

【问题讨论】:

    标签: node.js postgresql express postgis bookshelf.js


    【解决方案1】:

    您是否尝试过使用 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()
    

    【讨论】:

    • 它不起作用。我有这个错误:“UnhandledPromiseRejectionWarning: Unhandled Promise Rejection (rejection id: 2): error: select "organizations".* from "organizations" where ST_DWithin(lat_lon, ST_GeomFromText("POINT(45.43 10.99)", 4326), 1000 ) - 列“POINT(45.43 10.99)”不存在”。我的专栏叫“lat_lon”,是一个几何体。
    • 如果我这样做,它会起作用:'ST_DWithin(lat_lon, ST_MakePoint(45.43,10.99),1000)'。有什么区别?
    【解决方案2】:

    我发现whereRaw是我遇到类似情况时正在寻找的解决方案。

    基本示例

    如果我们使用where进行以下查询

    qb.where('id', 2')
    

    whereRaw 等效项是

    qb.whereRaw('id = ?', [2])
    

    应用于问题中的情况

    我相信这大致相当于你的查询

    qb.whereRaw('ST_DWithin(lat_lon, ST_GeomFromText("POINT(45.43 10.99)", 4326), 1000 )') 
    

    可能被参数化为

    qb.whereRaw(
        'ST_DWithin(lat_lon, ST_GeomFromText("POINT(?, ?)", 4326), ?)',
        [45.43, 10.99, 1000]
    ) 
    

    如果经度、纬度或搜索半径发生变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多