Home:ALL Converter>NodeJS MYSQL Query result Buffer?

NodeJS MYSQL Query result Buffer?

Ask Time:2016-10-28T19:00:36         Author:kimyeonho

Json Formatter

mysql select query result ...

//nodejs database.js
var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'me',
    database : 'test',
    password : '1234'
});

// nodejs app.js
app.get('/api/v0.1/getPostList', function(req, res) {
        limit_count = 5;
        db.query(postModel.postList(limit_count) , function(err, rows) {
            if (err) throw err;
            console.log(rows)
            res.json(rows);
        });
    });

//result
RowDataPacket {
    POST_SEQ: 13,
    POST_TYPE: <Buffer 31 31 30 30>,
    CATEGORY: <Buffer 49 54 20 2f 20 4d 4f 42 49 4c 45>, ...

why query data buffer type?
I do not know the cause.

java query data success
[DEBUG][2016-10-28 19:20:24,160] <== Row: 13, 1100, GAME ...

Author:kimyeonho,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40303670/nodejs-mysql-query-result-buffer
Ekjot Kaur :

I solved this issue by accessing values:\n\nrows[0].POST_TYPE.toString('utf8');\n rows[0].CATEGORY.toString('utf8');",
2017-01-07T06:23:22
Taking hours :

but this would cause performance issue if you are having a lot of records and fields. You would have to iterate and tostring all buffer fields.\nIt should receive data in the predefined datatypes. ie VARCHAR -> String",
2017-11-15T09:08:00
Nick Dallett :

I had this issue with int values generated by COUNT. I was able to resolve the issue by casting these values to CHAR within my query. They then came back correctly formatted as js strings. Here's a sample query:\n\nSELECT Date, Park, COUNT(FileID) FROM SkatePix WHERE Park != '' AND Date != 0 GROUP BY Date, Park LIMIT 5;\n\nI changed the COUNT statement to be:\n\n CAST(COUNT(FileID) AS CHAR) /* formerly COUNT(FileID) */\n\nThis did the trick. ",
2018-06-09T03:18:46
yy