这是我的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.multiLine
是MultiLineString
,并且我的表格中有一个几何列。
即使我已使用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));
答案 0 :(得分:1)
第一个问题是您将重定向两次重定向到属性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';
};
P.S。我仍然看到您的代码有问题。如果未设置该值,则将为该列返回NULL
,而在表中,该值明确定义为非NULL:st_astext geometry not null
。这是您需要解决的矛盾。