Why does node-firebird output a byte buffer instead of a string value?

1.6k Views Asked by At

I'm new to node.js and javascript studies and I'm trying to build a backend to fetch information from a Firebird database.

I am using some dependencies that are in a course that I am following. My project as 'express', 'nodemon', 'node-firebird'.

Using the following structure:

Project structure

The code in my server.js file is:

const firebird = require('node-firebird');
const express = require('express');
const app = express();
//const teste;

const options = {
  host: '127.0.0.1',
  database: 'C:/Users/alexandrefelix.GREENCANE/Desktop/Aulas/backend-firebird/src/TGA.FDB',
  user: 'SYSDBA',
  password: 'masterkey',
};


app.get('/', (req, res) => {

  // 5 = the number is count of opened sockets
  var pool = firebird.pool(5, options);

  // Get a free pool
  pool.get(function(err, db) {

    if (err)
        throw err;

    // db = DATABASE
    db.query('SELECT * FROM GEMPRESA', function(err, result) {
        // IMPORTANT: release the pool connection
        console.log(result);
        db.detach();
    });

  });

  // Destroy pool
  pool.destroy();

  return res.json({ message: 'Hello World!'});

});


app.listen(3333);

So when I run the code, and access 'localhost: 3333' in the browser it presents my test message (Hello World) but in the console, it presents the varchar fields as buffers:

CITY: Buffer 55 42 45 52 41 42 41

When it should be:

CITY: "UBERABA"

My question is why the returning JSON is displaying VARCHAR fields this way and how do I put the result of a column within a variable.

3

There are 3 best solutions below

1
Horatiu Jeflea On

result is actually a resultSet which needs to be parsed

result.forEach( function(row) {
   console.log( row.id, ab2str(row.name)); //id and name are fields from the select *
});

function ab2str(buf) {
   return String.fromCharCode.apply(null, new Uint16Array(buf));
}

More info: https://github.com/hgourvest/node-firebird/wiki/Example-of-querying-using-Promises

0
viniciusalvess On

This is an alternative to convert all the buffer properties.

async function query(aSql, aParams = []){
    return new Promise( (resolutionFunc,rejectionFunc) => {
        Firebird.attach(options, function(err, db) {
 
            if (err){
                rejectionFunc(err);
            }
         
            // db = DATABASE
            db.query(aSql, aParams, function(err, result) {
                
                try{
                    if(err){
                        rejectionFunc(err);
                    }
                    
                    for (let i = 0; i < result.length; i++) {                        
                        for (const [key, value] of Object.entries(result[i])) {
                            if(Buffer.isBuffer(value)){
                                result[i][key] =  `${value}`; // converting buffer to string
                            }
                        }
                    }

                    resolutionFunc(result);
                }finally{
                    db.detach();
                }                
            });         
        });
    });
}
0
Wellington Amorim On

I was having the same error. I solved it easily. When creating the table in the database, where I had VARCHAR, I added the CHARACTER SET WIN1251. For example:

  CREATE TABLE "PRODUCTS"
(
   "ID" INTEGER NOT NULL,
   "NAME" VARCHAR(50) CHARACTER SET WIN1251,
CONSTRAINT "PK_PRODUCTS" PRIMARY KEY ("ID")
);