0

This is my first time connecting to a Firebird database from Node JS. I'm using the "node-firebird" package. Everything seems to work fine. But I get these <Buffer 50 5a 41> tags back in my object.

This is what my query returns:

[
  {
    ARTICULO_ID: 422698,
    NOMBRE: 'CONEXION 3/4 HHP-S',
    ES_ALMACENABLE: <Buffer 53>,
    ES_JUEGO: <Buffer 4e>,
    ESTATUS: <Buffer 41>,
    CAUSA_SUSP: null,
    FECHA_SUSP: null,
    IMPRIMIR_COMP: <Buffer 4e>,
    PERMITIR_AGREGAR_COMP: null,
    LINEA_ARTICULO_ID: 785107
  }
]

And here is my code:

const pool = require("./util/database");

const query = "SELECT * FROM articulos WHERE nombre='CONEXION 3/4 HHP-S'";

pool.get(function (err, db) {
  if (err) throw err;

  db.query(query, function (err, result) {
    if (err) {
      console.log(err);
    }
    console.log(result);

    db.detach();
  });
});

pool.destroy();

Edit: The columns that return a Buffer are all string datatypes. The solution was buffer.toString()

dtremp
  • 21
  • 1
  • 3
  • 1
    Which Node.js driver are you using (there is more than one driver for Firebird)? What are the datatypes of ES_ALMACENABLE, ES_JUEGO, ESTATUS and IMPRIMIR_COMP? – Mark Rotteveel Apr 29 '22 at 07:24
  • what is there in your package, docs or examples, to work with BLOBs ? – Arioch 'The Apr 30 '22 at 18:33
  • As far as I'm aware, it is a Node.js [Buffer](https://nodejs.org/api/buffer.html), so if it is binary data, use `buf.toString('base64')`, if it is textual data, then you probably need to use `buf.toString('utf8')`. However, I rarely program in Node.js, so I might be wrong. That said, you really need to specify in your question the actual data types of those columns, that helps us to try things out ourselves. – Mark Rotteveel May 01 '22 at 10:10
  • Looks like BLOB ID. Do you know exact data type of the column IMPRIMIR_COMP? Is it BLOB? If so, there should be some functions in the library to read BLOB data given its ID. – Andrej Kirejeŭ May 03 '22 at 17:31
  • @MarkRotteveel I was away from work, so I didn't have a chance to see what the datatypes were. But they are all strings. I tried your solution buf.toString(), and it works. I'm not sure what I did wrong before. Thanks for the help. I will post the solution. – dtremp May 04 '22 at 18:12
  • @dtremp Firebird doesn't have "string" as datatype as such, it has CHAR, VARCHAR or BLOB SUB_TYPE TEXT. At a guess, whether it returns buffer or a strings is informed by the exact data type and/or its character set. – Mark Rotteveel May 05 '22 at 07:58

1 Answers1

1

It seems that the query was only returning Buffers for string datatypes. I used this code to fix it:

//After query returns a result.

const product = result[0]; //Pull out the first result

for (let key in product) {
   currentDataset = product[key];
   if (Buffer.isBuffer(currentDataset) === true) {
      product[key] = currentDataset.toString('utf-8');
   }
} 

My problem was lack of understanding how Buffers work. Thanks to @Mark Rotteveel for the advice.

dtremp
  • 21
  • 1
  • 3
  • Good to hear you managed to fix it. Be aware though, that generically converting buffers using utf-8 can be problematic if the column is a binary type (e.g. (VAR)CHAR CHARACTER SET OCTETS (or (VAR)BINARY since Firebird 4.0) or BLOB SUB_TYPE BINARY (or any other sub-type other than TEXT). – Mark Rotteveel May 05 '22 at 08:00