Node-firebird Reading Blob

371 views
Skip to first unread message

NGC 147

unread,
Sep 20, 2023, 4:39:24 AM9/20/23
to firebird-support
Hello. I'm using Firebird 2.5.9.
I'm writing my server in ExpressJS.
The database has a table with photographs. But they are of type "BLOB SUB_TYPE BINARY".
I don't understand how I can get this photo, which consists of a byte array. I need to get it by sending a GET request to the address..
Help please, I don't understand anything.

Look what I have.
2023-09-20_11-30-09.png
2023-09-20_11-31-29.png
2023-09-20_11-34-41.png
Everything is in the database and the script works
2023-09-20_11-37-23.png

Dimitry Sibiryakov

unread,
Sep 20, 2023, 4:43:52 AM9/20/23
to firebird...@googlegroups.com
NGC 147 wrote 20.09.2023 10:38:
> Help please, I don't understand anything.

From Google:

https://snyk.io/advisor/npm-package/node-firebird-driver/functions/node-firebird-driver.Blob

GET request can return binary data without problem if you set right response
headers (mime type and encoding).

--
WBR, SD.

NGC 147

unread,
Sep 20, 2023, 4:45:51 AM9/20/23
to firebird-support
I saw this link. Thank you.
But I don't understand anything. Can you please describe in detail what is happening there. I'm new to this and have never worked with firebird

среда, 20 сентября 2023 г. в 11:43:52 UTC+3, sd:

Dimitry Sibiryakov

unread,
Sep 20, 2023, 4:47:52 AM9/20/23
to firebird...@googlegroups.com
NGC 147 wrote 20.09.2023 10:45:
> But I don't understand anything. Can you please describe in detail what is
> happening there. I'm new to this and have never worked with firebird

In this case you should start from reading of a basic documentation for
chosen programming language and tools instead of writing to (wrong) support mail
lists.

--
WBR, SD.

Mark Rotteveel

unread,
Sep 20, 2023, 4:49:32 AM9/20/23
to firebird...@googlegroups.com
Please include your code as text, not as images. I guess you're using
https://github.com/hgourvest/node-firebird, is that right?

When using that driver, the result for a blob is not simply a buffer.
Have you read their example for reading blobs at
https://github.com/hgourvest/node-firebird#reading-blobs-asynchronous ?

See also these Stack Overflow questions:

* https://stackoverflow.com/questions/47675004/select-blob-return-a-function
*
https://stackoverflow.com/questions/46574826/firebird-blob-to-base64-node-js

Mark

On 20-09-2023 10:38, NGC 147 wrote:
> Hello. I'm using Firebird 2.5.9.
> I'm writing my server in ExpressJS.
> The database has a table with photographs. But they are of type "BLOB
> SUB_TYPE BINARY".
> I don't understand how I can get this photo, which consists of a byte
> array. I need to get it by sending a GET request to the address..
> Help please, I don't understand anything.
>
> Look what I have.
> 2023-09-20_11-30-09.png
> 2023-09-20_11-31-29.png
> 2023-09-20_11-34-41.png
> Everything is in the database and the script works
> 2023-09-20_11-37-23.png
>
> --
> You received this message because you are subscribed to the Google
> Groups "firebird-support" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to firebird-suppo...@googlegroups.com
> <mailto:firebird-suppo...@googlegroups.com>.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/firebird-support/3a67c801-09cf-45d6-8a9a-47f8b01ea979n%40googlegroups.com <https://groups.google.com/d/msgid/firebird-support/3a67c801-09cf-45d6-8a9a-47f8b01ea979n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Mark Rotteveel

NGC 147

unread,
Sep 20, 2023, 5:04:03 AM9/20/23
to firebird-support
That's right. I'm using node-firebird
Please provide it in text.

I tried this, but I still get an error. Please, I'm asking for your help.
 -------------------------------------------------------------------------------------------------
app.get("/photo/:id", (req, res) => {
  const id = req.params.id;
  const ssql = "SELECT fotob FROM FOTO WHERE ID_FOT = ?"

  executeQuery(ssql, [id], (err, result) => {
    if (err) {
      console.error("Err:", err);
      res.status(500).send("Internal Server Error");
    } else if (result.length === 0) {
      res.status(404).send("Product not found");
    } else {
      result[0].FOTOB(function (err, name, eventEmitter) {
        var buffers = [];
        eventEmitter.on('data', function (chunk) {
          buffers.push(chunk);
        });
        eventEmitter.once('end', function () {
          var buffer = Buffer.concat(buffers);
          retorno = (buffer.toString());
          db.detach();
          cb(retorno)
        });
      });
    }
  });
});

2023-09-20_11-59-57.png

среда, 20 сентября 2023 г. в 11:49:32 UTC+3, ma...@lawinegevaar.nl:

Dimitry Sibiryakov

unread,
Sep 20, 2023, 5:06:41 AM9/20/23
to firebird...@googlegroups.com
NGC 147 wrote 20.09.2023 11:04:
> I tried this, but I still get an error. Please, I'm asking for your help.

Programming is not a job which you can done by random typing and copy-pasting.
What is not clear for you in the message "FOTOB is not a function"?

--
WBR, SD.

NGC 147

unread,
Sep 20, 2023, 5:08:12 AM9/20/23
to firebird-support
Because the example contains the same line.
rows[0].userpicture(function(err, name, e) {
and here it is a function.

среда, 20 сентября 2023 г. в 12:06:41 UTC+3, sd:

NGC 147

unread,
Sep 20, 2023, 5:10:32 AM9/20/23
to firebird-support
If I didn't have mistakes, I wouldn't write here. But I did everything according to the documentation and it didn’t work.

среда, 20 сентября 2023 г. в 12:08:12 UTC+3, NGC 147:

Dimitry Sibiryakov

unread,
Sep 20, 2023, 5:15:26 AM9/20/23
to firebird...@googlegroups.com
NGC 147 wrote 20.09.2023 11:10:
> If I didn't have mistakes, I wouldn't write here. But I did everything according
> to the documentation and it didn’t work.

But it is your mistakes and fixing error in code is a big part of every
programmer's work. Basic learning is not something that should be done in
support list.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function


--
WBR, SD.

Mark Rotteveel

unread,
Sep 20, 2023, 6:17:53 AM9/20/23
to firebird...@googlegroups.com
On 20-09-2023 11:04, NGC 147 wrote:
> I tried this, but I still get an error. Please, I'm asking for your help.
[..]
>     } else {
>       result[0].FOTOB(function (err, name, eventEmitter) {

JavaScript is case-sensitive. Have you tried with fotob instead
(lowercase, not uppercase), just like you used in your original code
(when you received an error that it *was* a function)?

[..]
>         eventEmitter.once('end', function () {
>           var buffer = Buffer.concat(buffers);
>           retorno = (buffer.toString());

Given you expect binary data, using toString() is probably incorrect as
that will result in a lossy conversion using UTF-8.

Mark
--
Mark Rotteveel

Tomasz Tyrakowski

unread,
Sep 20, 2023, 7:16:54 AM9/20/23
to firebird...@googlegroups.com
On 20.09.2023 at 11:10, NGC 147 wrote:
> If I didn't have mistakes, I wouldn't write here. But I did everything
> according to the documentation and it didn’t work.

Just a suggestion: solve two sub-problems independently. First, in your
express app, read an image (always the same image) from a local file and
send it back to the browser. Make it work so that you can actually see
the image in the browser. This subtask is database-independent.
Next, write a simple toy node app, which will read a single image data
as a blob from your database and save it to a local file. Make it work
so that when you click the file, you can actually open and view the image.
Finally, combine techniques you use in both subtasks to solve your
original problem.
I think you might also need the mime type of the image, unless they're
all in the same format (you'll probably discover it solving the first
sub-problem).

AFAIR, when reading blobs using node-firebird, I've always done it like
this:

data_buffer = await db.getBlobValue(resultset[0].field_name);

where "field_name" is the name of the blob field you select in your
query. However, I've used KOA (an async clone of express), so I could
await getBlobValue, because route handlers in KOA are async functions;
perhaps you need to use promise api to read the blob (i.e.
db.getBlobValue(...).then(...)) if your express route handlers are not
async.

regards
Tomasz

NGC 147

unread,
Sep 20, 2023, 7:23:04 AM9/20/23
to firebird-support
The issue is resolved. I've already figured it out.
 Here is the finished code, it may be useful to someone.

File index.js
____________________________________
app.get("/photo/:id", (req, res) => {
  const id = req.params.id;

  getBlobImage(id, res);
});
____________________________________


File database.js
____________________________________
function getBlobImage(id, res) {
    firebird.attach(dbOptions, function (err, db) {
        if (err) throw err;

        db.query('SELECT FOTOB FROM FOTO WHERE ID_FOT = ?', [id], function (err, result) {
            if (err) throw err;

            result[0].fotob(function (err, name, e) {
                var buffers = [];
                e.on('data', function (chunk) {
                    buffers.push(chunk);
                });
                e.on('end', function () {
                    var buffer = Buffer.concat(buffers);
                    db.detach();

                    res.setHeader('Content-Type', 'image/jpeg');
                    res.end(buffer);
                });
            });
        });
    });
}
export { getBlobImage };
____________________________________

Thanks to all

среда, 20 сентября 2023 г. в 14:16:54 UTC+3, Tomasz Tyrakowski:

Dimitry Sibiryakov

unread,
Sep 20, 2023, 7:30:50 AM9/20/23
to firebird...@googlegroups.com
NGC 147 wrote 20.09.2023 13:23:
> The issue is /resolved/. I've already figured it out.
>  Here is the finished code, it may be useful to someone.

See? This is exactly what was suggested to you:

1) Set right content type header
2) Use right case for variable/function name

But all this you figured out yourself so help from support was not really
needed. Congratulation, you are one step closer to being a programmer.

--
WBR, SD.
Reply all
Reply to author
Forward
0 new messages