这是我的PostgreSQL表:

create table scheduled_locations
(
  id           uuid     not null,
  device_id    text     not null,
  user_id      uuid,
  created_date date,
  st_astext    geometry not null
    constraint scheduled_locations_idkey
    primary key
);

alter table scheduled_locations
  owner to postgres;

create index gist_idx
  on scheduled_locations (st_astext);


通过nodejs,我想填写表格。这是我想存储到此表中的json对象:

const dataSingle = {
    'id': uuidv1(),
    'device_id': user.device_id,
    'user_id': user.user_id,
    'created_date': user.date,
    'st_astext': user.multiLine
};


user.multiLine具有此值:

user.multiLine
Object {type: "Feature", properties: Object, geometry: Object}
geometry:Object {type: "MultiLineString", coordinates: Array(23)}
properties:Object {}
type:"Feature"
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}


通过使用pg-promise我想保存到postgresql中:

await db.any(pgp.helpers.insert(dataSingle, null, 'scheduled_locations'));


但是我得到了这个错误:

Error: parse error - invalid geometry
code:"XX000"
column:undefined
constraint:undefined
dataType:undefined
detail:undefined
file:"lwgeom_pg.c"
hint:""{"" <-- parse error at position 2 within geometry"
internalPosition:undefined
internalQuery:undefined
length:151
line:"92"
message:"parse error - invalid geometry"
name:"error"
position:"230"
routine:"pg_parser_errhint"
schema:undefined
severity:"ERROR"
stack:"error: parse error - invalid geometry\n    at Connection.parseE (e:\Projects\NodeJs\HistoricalLogPg\node_modules\pg\lib\connection.js:553:11)\n    at Connection.parseMessage (e:\Projects\NodeJs\HistoricalLogPg\node_modules\pg\lib\connection.js:378:19)\n    at Socket.<anonymous> (e:\Projects\NodeJs\HistoricalLogPg\node_modules\pg\lib\connection.js:119:22)\n    at Socket.emit (events.js:182:13)\n    at addChunk (_stream_readable.js:283:12)\n    at readableAddChunk (_stream_readable.js:264:11)\n    at Socket.Readable.push (_stream_readable.js:219:10)\n    at TCP.onread (net.js:639:20)"
table:undefined
where:undefined


我是gis的新手,但为什么会出现此错误? user.multiLineMultiLineString,我的表有一个geometry列。

即使我已经使用pg-promise列,但仍然有上述错误:

const getPoint = col => {
    const p = col.source;
    return p ? pgp.as.format('st_astext(${multiLine})', p) : 'NULL';
};

const cs = new pgp.helpers.ColumnSet([

    {
        name: 'id',
        mod: ':raw',
        init: generate_id
    },
    'device_id',
    'user_id',
    {
        name: 'created_date',
        prop: 'date'
    },
    {
        name: 'st_astext',
        prop: 'multiLine',
        mod: ':raw',
        init: getPoint
    }
], {
    table: 'scheduled_locations'
});

let results = await db.any(pgp.helpers.insert(user, cs));

最佳答案

第一个问题是您将重定向到属性multiLine中,即首先告诉ColumnSet值在multiLine中,然后在格式化逻辑中重新执行该操作。

将此列替换为:

{
    name: 'st_astext',
    mod: ':raw',
    init: getPoint
}


然后,我猜测您要检查是否设置了值,而不是是否检查了整个对象。因此,getPoint的正确实现应为:

const getPoint = col => {
    const p = col.source.multiLine;
    return p ? pgp.as.format('st_astext($1)', p) : 'NULL';
};


附言我仍然看到您的代码有问题。当未设置该值时,您将为该列返回NULL,而在表中,该值明确定义为非NULL:st_astext geometry not null。这是您需要解决的矛盾。

10-06 08:27