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

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

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 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