Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda