Mysql: Why select result return an array of RowDataPacket?

Created on 13 Jan 2016  路  2Comments  路  Source: mysqljs/mysql

I am wondering why the query result for a SQL SELECT return something like this:

[ RowDataPacket {
        id: 19,
        username: 'admin',
        status: 1,
        role: 2,
        phone: '123456789',
        first_name: 'admin',
        last_name: 'admin' },
      RowDataPacket {
        id: 23,
        username: 'abhjj',
        status: 1,
        role: 2,
        phone: '123456789',
        first_name: 'admin',
        last_name: 'admin' },
      RowDataPacket {
        id: 24,
        username: 'abheee',
        status: 1,
        role: 2,
        phone: '123456789',
        first_name: 'admin',
        last_name: 'admin' } ] 

Shouldn't it just return something a "pure" JSON array like this:

[{"id":19,"username":"admin","status":1,"role":2,"phone":"123456789","first_name":"admin","last_name":"admin"},{"id":23,"username":"abhjj","status":1,"role":2,"phone":"123456789","first_name":"admin","last_name":"admin"},{"id":24,"username":"abheee","status":1,"role":2,"phone":"123456789","first_name":"admin","last_name":"admin"}]

Maybe I am new to Javascript but the one returned by node-mysql doesn't even qualified as JSON object? no colon after RowDataPacket?

question

Most helpful comment

Hi @vthai ! 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.

Try using this: console.log(JSON.stringify(result))

All 2 comments

Hi @vthai ! 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.

Try using this: console.log(JSON.stringify(result))

It might actually help with performance as well. Because all rows are instances of the same RowDataPacket class, they all share same 'hidden class' - see for example http://s3.mrale.ph/nodecamp.eu/#41 for explanation

Was this page helpful?
0 / 5 - 0 ratings

Related issues

macias picture macias  路  3Comments

winzig picture winzig  路  4Comments

abou7mied picture abou7mied  路  4Comments

skilbjo picture skilbjo  路  3Comments

nanom1t picture nanom1t  路  3Comments