Reputation: 49
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 ...
Upvotes: 4
Views: 7471
Reputation: 11
This topic is very old.
However, I do not have a good answer anywhere.
I found good solution. So I reply this topic
reference (npm - mysql) https://www.npmjs.com/package/mysql#type-casting
// Modify the typeCast option to suit your situation.
createConnection({
host: config.host,
port: config.port,
user: config.user,
password: config.password,
database: config.database,
multipleStatements: true,
typeCast: function (field, next) {
if (field.type == 'VAR_STRING') {
return field.string();
}
return next();
}
})
Upvotes: 1
Reputation: 41
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:
SELECT Date, Park, COUNT(FileID) FROM SkatePix WHERE Park != '' AND Date != 0 GROUP BY Date, Park LIMIT 5;
I changed the COUNT statement to be:
CAST(COUNT(FileID) AS CHAR) /* formerly COUNT(FileID) */
This did the trick.
Upvotes: 4
Reputation: 51
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. It should receive data in the predefined datatypes. ie VARCHAR -> String
Upvotes: 1
Reputation: 21
I solved this issue by accessing values:
rows[0].POST_TYPE.toString('utf8');
rows[0].CATEGORY.toString('utf8');
Upvotes: 2