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?
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
Most helpful comment
Hi @vthai ! We return an array of
RowDataPacketobjects 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 usedconsole.dirorconsole.log, which Node.js uses a different formatting.Try using this:
console.log(JSON.stringify(result))