3

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1) This driver development seems stalled - https://github.com/hgourvest/node-firebird/commits/master - but there was some another FB3-centered NodeJS driver. 2) are you sure your field is VARCHAR not BLOB SUB_TYPE TEXT ? 3) it has to be checked that connection to firebird has character set and that the VARCHAR column has character set too. If any of those is `NONE` or `OCTET` then raw binary dump would be what you have to get. Granted, `node-firebird` documentation says they always do `UTF-8` connection, still 3a) DB schema to be checked. 3b) `select MON$CHARACTER_SET_ID from MON$ATTACHMENTS` – Arioch 'The Oct 09 '19 at 14:03
  • dunno what `meta` would be but https://github.com/hgourvest/node-firebird/wiki/Example-of-getting-metadata – Arioch 'The Oct 09 '19 at 14:06

3 Answers3

4

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

Horatiu Jeflea
  • 7,256
  • 6
  • 38
  • 67
  • 2
    There is also a page specifically addressing it: https://github.com/hgourvest/node-firebird/wiki/Converting-results-from-arraybuffers-to-string-values – Mark Rotteveel Oct 09 '19 at 15:17
0

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();
                }                
            });         
        });
    });
}
viniciusalvess
  • 756
  • 8
  • 18
0

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")
);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83