7

This is my simple statement:

var mysql = require("mysql");
var CONCDB = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "1234",
  database: "pwc"
})

CONCDB.connect();

CONCDB.query("SELECT first_name FROM users LIMIT 1", function(err, rows, fields) {
  if (err) throw err;
    console.log("The solution is: ", rows);
});

Why I get as result:

[ RowDataPacket { first_name: <Buffer 47 61 62 72 69 65 6c> } ]

If I use

... function(err, results) {
  ...
  console.log("The solution is: ", results)

I get the same as using rows.

Using function(err, rows, fields) and returning fields I get the following:

[ FieldPacket {
  catalog: 'def',
  db: 'pwc',
  table: 'users',
  orgTable: 'users',
  name: 'first_name',
  orgName: 'first_name',
  charsetNr: 63,
  length: 30,
  type: 253,
  flags: 4225,
  decimals: 0,
  default: undefined,
  zeroFill: false,
  protocol41: true } ]

Sometimes this statement throw:

[Object object]

What am I doing wrong? I cant get the right result. Someone can help me, I am following the same code as others but why this throw me an error?

For example, trying with this: (I saw this in a website)

CONCDB.query('SELECT * from users', function(err, rows, fields) {
  if (!err)
    console.log('The solution is: ', rows);
  else
    console.log('Error while performing Query.');
});

The result is:

The solution is:  [ RowDataPacket {
    id: 1,
    first_name: <Buffer 47 61 62 72 69 65 6c>,
    last_name: <Buffer 50 65 72 65 69 72 61> } ]

3 Answers 3

16

[Object object]

is not an error , its just telling you the typedef of results.

For printing the value that is contained inside results just try using only:

console.log(results)

and not

console.log("the solution is "+results)

OR if want a deep print

console.log(JSON.stringify(results))
Sign up to request clarification or add additional context in comments.

3 Comments

Hello wrangler, thank you for your answer. This is the result using console.log(results): [ RowDataPacket { first_name: <Buffer 47 61 62 72 69 65 6c> } ]
Simple first_name. The "the solution is" is just example to complete the statement.
@zagk have you checked typeof firstname in sql table??
2
console.log(results);

this worked fine for me, but I wanted to use the results in a nested query, example:

results[0].someAttribute

for anybody coming from google.

Comments

1

I wrote only console.log(results)

This solved my problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.