Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
Storing JSON objects in the postgreSQL using parameterized query

I am having problems with saving JSON objects in the database using parameterized queries. I am using postman to send this object on red.body enter image description here

On my server side I havet his code:

queryController.createQuery = (req, res, next) => {
  const { info }= req.body;
  const sqlQuery = 'INSERT INTO test (info) VALUES ($1) RETURNING *';
  db.query(sqlQuery, [info])
  .then(result => {
    console.log(result);
    if (result) res.locals.message = "Saved the query successfully";
    next();
  })
  .catch(err => {
    return next({
      log: `queryController.createQuery: ERROR: ${typeof err === 'object' ? JSON.stringify(err) : err}`,
      message: { err: 'Error occurred in queryController.createQuery. Check server log for more details.'},
    })
  })

Why is that I still get this error: enter image description here

throw new TypeError('Client was passed a null or undefined query')
      ^

TypeError: Client was passed a null or undefined query

Here is my table schema:

CREATE TABLE test (
    id serial NOT NULL PRIMARY KEY,
    info json NOT NULL
);

How do I format $1 correctly to get it accept it as JSON file?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Query example:

EXECUTE 'INSERT INTO test (info) VALUES ($1) RETURNING *' USING '[{"size": "Small", "quantity": 1, "product_id": 1}]'::jsonb;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda