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

119
Visualizações
Node.js, pg (postgresql) - put pure binary data into 'bytea' field

my question is: what is right way to put binary data into 'INSERT' PostgreSQL (Node.js + pg) request ?

const req = "INSERT INTO public.product (" +
            "name, doc_name, purchase_price, selling_price, weight, " +
            "id_string, landing_url, img_data, img_name,  active) " +
            "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);";

const values = [
  obj.product_name, obj.product_doc_name,
  obj.product_purchase_price, obj.product_selling_price, obj.product_weight,
  obj.product_id_string, obj.product_landing_url,
  obj.product_image_data, obj.product_image_name, 'true'
];

'obj.product_image_data' is the Express request body field('req.body.product_image_data');

My 'test' store binary data (from 'req.body.product_image_data') to the local server side file is OK.

console.log("product_image_data lenght: " + obj.product_image_data.length);
//write img data to file
fs.writeFileSync('./TEST.PNG', obj.product_image_data, 'binary');

Of Course, pure binary data 'as is' has error result as on the picture. Image of error

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

A regular Node.js buffer of binary data should work for node pg and column type bytea.

> Buffer.from('\x00\x01\x02')
<Buffer 00 01 02>

Make sure the object is a proper binary string as a buffer. Using Buffer.from might do the trick.

const values = [
  obj.product_name, obj.product_doc_name,
  obj.product_purchase_price, obj.product_selling_price, obj.product_weight,
  obj.product_id_string, obj.product_landing_url,
  Buffer.from(obj.product_image_data), obj.product_image_name, 'true'
];
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