Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Prepared insert statement in node.js using pg and postgresqlDB

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. :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!