I'm trying to write a Polygon to my Postgresql DB. Postgis is installed and I tried to execute an example Statement and it's working. So far so good.
Here's my example:
INSERT INTO assets VALUES('test', 'test2id', 210, ST_Polygon('LINESTRING(16.0 14.0, 20.0 14.0, 20.0 12.0, 16.0 12.0,16.0 14.0)':: geometry, 4326), 'Crime');
So I tried to convert this in a prepared statement and my code looks like this:
const Pool = require('pg').Pool
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
})
const sql = "INSERT INTO assets (title, id, duration, geodata, genre) VALUES($1, $2, $3, $4, $5);"
const values = [title, crid, duration, poly_string, genre]
pool.query(sql, values, (error, results) => {
if (error) {
response.status(500).send("Internal Server Error")
throw error
}
})
The values for the variables are like that:
poly_string = "ST_Polygon('LINESTRING(16 14, 20 14, 20 12, 16 12, 16 14)':: geometry, 4326)"
title = "test"
id = "IHSJSKLANSHKLSHJSMSNSD"
duration = 1290
genre = "crime"
If I run the code I get the following error:
error: parse error - invalid geometry
length: 147,
severity: 'ERROR',
code: 'XX000',
detail: undefined,
hint: '"ST" <-- parse error at position 2 within geometry',
Thankfull for any help or ideas. :)
Hy Oivalf, I have same case, but on my case is for query Update, here my example query
const updatePole =
'UPDATE public."Pole" SET geom = $1, status = $2, WHERE id = $3'
when I working on form data on table It's work well, but when I change on maps it's not work, and make data value to be null. I was try using this code
const updatePole =
'UPDATE public."Pole" SET geom = ST_AsGeoJSON($1,4326::integer), status = $2, WHERE id = $3'
and it's working for me.