question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strings are converted to the data type of buffer

See original GitHub issue

Hello! I have the following table:

CREATE TABLE GROUP_ORDER (
    ID                 INTEGER NOT NULL,
    SOURCE_ID          INTEGER NOT NULL,
    NUMBER             VARCHAR(200) NOT NULL,
    IS_OPTIMIZED       INTEGER NOT NULL,
    "DATE"             TIMESTAMP NOT NULL,
    NUMBER_ORDER_LIST  VARCHAR(5000)
);

I make this request:

select * from GROUP_ORDER

And I get this answer:

[ { ID: 2,
    SOURCE_ID: 5,
    NUMBER: <Buffer 31>,
    IS_OPTIMIZED: 0,
    DATE: 2017-01-16T22:00:00.000Z,
    NUMBER_ORDER_LIST: <Buffer 30 32 36 33 31 5f 30 32 36 31 34> },
  { ID: 534,
    SOURCE_ID: 3,
    NUMBER: <Buffer 33>,
    IS_OPTIMIZED: 1,
    DATE: 2016-04-19T22:00:00.000Z,
    NUMBER_ORDER_LIST: <Buffer 31 36> },
  { ID: 539,
    SOURCE_ID: 9,
    NUMBER: <Buffer 42 49 47 5f 31>,
    IS_OPTIMIZED: 1,
    DATE: 2016-05-21T22:00:00.000Z,
    NUMBER_ORDER_LIST: <Buffer 31 2c 20 32 2c 20 33> },
  { ID: 540,
    SOURCE_ID: 10,
    NUMBER: <Buffer 42 49 47 5f 32>,
    IS_OPTIMIZED: 1,
    DATE: 2016-05-21T22:00:00.000Z,
    NUMBER_ORDER_LIST: <Buffer 31 32 33 2c 20 35 2c 20 36> } ]

Why, instead Strings Variables I have variables of type Buffer (see fields NUMBER and NUMBER_ORDER_LIST)?

On database this one in the form of a buffer to another as a string.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
e-baroncommented, Oct 10, 2018

Hello, I have used this solution in order to decode buffer objects:

  1. Find out what is the default character set of your DB. I did not know, so I tested those listed here https://nodejs.org/api/buffer.html (utf8, latin1…)
  2. Use function toString( ‘your character set’) on the buffer objects.

Here is an example of code illustrating how to print to the console the string encoded in the buffer:

db.query('SELECT FIRST 10 NOM,PRENOM FROM ETUD', function(err, result) {
            if (result != undefined)
                { 
                for (i=0; i< result.length ; i++){
                    /* string / text in firebird are converted to buffer objects by NodeJS. Those buffer are encoded in latin1, apparently the default character set of our DB */  
                    console.log("Student: " , result[i].NOM.toString('latin1'), " ", result[i].PRENOM.toString('latin1'));
                }                

                }
            else
                console.log("No results");
            db.detach();
        });
5reactions
cesarbalbinottcommented, Sep 25, 2018

use: select ID, SOURCE_ID, cast(NUMBER as varchar(50) character set utf8) "NUMBER", IS_OPTIMIZED, DATE, cast(NUMBER_ORDER_LIST as varchar(50) character set utf8) "NUMBER_ORDER_LIST" from GROUP_ORDER

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert a String to Buffer and vice versa in Node.js
To convert a string to a Buffer, you can use the from() method from the global Buffer class in Node.js. // a string...
Read more >
Using the Buffer `toString()` Function in Node.js - Mastering JS
Node.js buffers are objects that store arbitrary binary data. Buffers have a toString() method that you can use to convert the buffer to...
Read more >
Convert Node.js Buffer to String: An Easy-To-Follow Guide
In a nutshell, it's easy to convert a Buffer object to a string using the toString() method. You'll usually want the default UTF-8...
Read more >
Converting a Buffer to JSON and Utf8 Strings in Nodejs
Buffers can convert to JSON. The JSON specifies that the type of object being transformed is a Buffer , and its data.
Read more >
Converting between strings and ArrayBuffers - Stack Overflow
If the data is a string. If it is UTF16 or a smaller encoding, it can be turned into an ArrayBuffer with this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found