I am using a mysql npm package for connection and trying to get data from table, which has columns with type BIT. But these columns have gone as:
"isBasic": {
"type": "Buffer",
"data": [
1
]
},
How can I map them to boolean type?
RowDataPacket is actually the name of the constructor function that creates an object, it would look like this new RowDataPacket(user_id, ...) . You can check by accessing its name [0].constructor.name. If the result is an array, you would have to use [0].
The bit data type is not perfectly used in mysql package. We usually use tinyint in tables to store 0 and 1 and then just do the comparison in Javscript to determine true or false.
Here is how you can read type=BIT value:
bit.lastIndexOf(1) !== -1
I've tested with true and false values:
console.log(bit, ' ----> ', bit.lastIndexOf(1) !== -1);
console.log(bit, ' ----> ', bit.lastIndexOf(1) !== -1);
// out: <Buffer 01> ' ----> ' true
// out: <Buffer 00> ' ----> ' false
And another way is to add transformation into SQL query:
mysql_connection.query('SELECT *,field_name=1 as field_name ...
and field_name's value will be 1 if true and 0 for false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With