MySQL query return [Object, object] in NodeJS, why?

nodejs mysql result to json
node.js mysql select query example
node-mysql return result
node js mysql query parameters
node.js mysql query loop
node js object object
select query with where clause in node js
how to fetch data from database in node js and display in html

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> } ]

[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);

MySQL query return [Object, object] in NodeJS, why?, Try this: <h1>Test</h1> <% data.forEach(function(item) { %> <p> <strong><%= item %></strong></p> <% }); %>. When a MySQL Query is executed in Node.js, an object called Result Object is returned to the callback function. The Result Object contains result set or properties that provide information regarding the execution of a query in MySQL Server. Node.js MySQL Result Object. The contents of Result Object depends on the SQL query made to MySQL Server.

I wrote only console.log(results)

This solved my problem.

Node.js returns [object Object] from MySQL query inside Javascript , Example. Select name and address from the "customers" table, and display the return object: var mysql = require('mysql'); var con = mysql.createConnection({ This uses real MySQL prepared statements, but unfortunately doesn’t let you work with arrays and objects. Running multiple queries on a single connection. Every time you call .query() or .execute(), you might get a new connection from the pool. Sometimes it’s a good idea to use the same connection if you do multiple queries.

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.

Node.js MySQL Select From, You can access the connection object if you want to use the queryFormat = function (query, values) { if (!values) return  Node.js MySQL Update - How to Update Data Using Node.js and MySQL. This tutorial will help you to update the data of the MYSQL table row. If you want to use MySQL with Node.js, it will be very useful for your project.…

Node.js with MySQL, The query results are returned as an array of objects with each row representing a single object in the array. So we need to iterate through the  As you can see from the result of the example above, the fields object is an array containing information about each field as an object. To return e.g. the name of the second field, just refer to the second array item's name property:

Querying MySQL with Node.js, A pure node.js JavaScript Client implementing the MySQL protocol. However, a connection can also be implicitly established by invoking a query: and DECIMAL columns) to be always returned as JavaScript String objects (Default: false ). We return an array of RowDataPacket objects because this is a low-level driver library. But regardless, the output you provided above is not JSON because you didn't use a JSON serializer to display it. It looks like you probably used console.dir or console.log, which Node.js uses a different formatting.

mysqljs/mysql: A pure node.js JavaScript Client , Some dialects return the metadata "within" the results object (as properties on an array). However, two arguments will always be returned, but for MSSQL and  (Binary data may contain the \0 character, which mysql_query() interprets as the end of the statement string.) If you want to know whether the statement returns a result set, you can use mysql_field_count() to check for this. See Section 28.7.6.22, “mysql_field_count()”.

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??