Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 589
Alexandre Felix Avatar asked Jul 24 '26 11:07

Alexandre Felix


1 Answers

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

like image 183
Horatiu Jeflea Avatar answered Jul 27 '26 04:07

Horatiu Jeflea