Problems creating API with Firebird Database and Node JS

Asked

Viewed 1,041 times

0

I am studying how to develop an API to query data from a Firebird database. To do this I am using the Node-Firebird, but the connection does not seem to be working.

Follows excerpt from the code:

const firebird = require('node-firebird');

var options = {};
 
options.host = '127.0.0.1';
options.port = 3050;
options.database = 'C:/PASTA/DADOS/BANCO.FDB';
options.user = 'SYSDBA';
options.password = 'masterkey';
options.lowercase_keys = false; // set to true to lowercase keys
options.role = null;            // default
options.pageSize = 4096;

const consulta = firebird.attach(options, (err, db) => {
    
    if (err){
       //throw err;
    }
    else{
        db.query('SELECT * FROM UCTABUSERS', function(err,result){
            try{
                console.log('A função funcionou');
                this.usuarios = result;
                db.detach();
            }catch{
                throw err;
            }
        });
    }

    return this.usuarios;
});

module.exports = consulta;

I use this function in another javascript file. The problem is that there is no message or error, only the return that is empty.

Does anyone have any idea what might be going on?

  • I solved this problem using another method from this library. I used the pool method to open the connection and query. Follow the code excerpt:

  • const Rows = async () => { Try{ Try{ var pool = client.pool(5, options); pool.get(Function(err, db) { Try{ db.query("SELECT * FROM UCTABUSERS Where UCIDUSER = 1", Function(err, result) { setResult(result); db.detach(); }); }catch{ console.log(err); Return err; } }); pool.Destroy();

  • console.log('Return': '+ getResult(); Return getResult(); }catch{ Return 'Unable to open connection'; } }catch{ Return"Unable to connect to database"; } };

  • Gustavo could post the complete code corrected, because I have the same problem, and as I am new with Node.js I could not understand where to change the code.

1 answer

0

The code I used to make it work was this one.

const express = require("express");

const router = express.Router();

router.get("/listagem", async (req, res) => {
  try {
    const client = require("../database/index");

    var options = require("../database/config");

    var pool = client.pool(5, options);

    pool.get(function(err, db) {
      try {
        db.query(
          "SELECT cast(CPF as varchar(18) character set win1252) as CGC_CPF, cast(cliente as varchar(45) character set win1252) as cliente FROM CLIENTE",
          function(err, result) {
            try {
              if (err) {
                return res
                  .status(400)
                  .send({
                    error:
                      "Ocorreu um erro ao tentar fazer a consulta de Clientes. Erro : " +
                      err
                  });
              }

              if (result != undefined) {
                return res.send(JSON.stringify(result));
              } else {
                res
                  .status(400)
                  .send({ error: "Não existem dados para ser retornados" });
              }
            } catch {
              res.status(400).send({ error: "Falha ao buscar dados no banco" });
            }
          }
        );
        db.detach();
      } catch {
        console.log("Ocorreu um erro ao gerar o pool conexões");
      }
    });
  } catch {
    res
      .status(400)
      .send({ error: "Falha ao carregar os dados de clientes do servidor" });
  }
});

module.exports = app => app.use("/clientes", router);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.