0

I am trying to convert the response after running knex queries into json so I can individual result

knex.raw("SELECT SUM(`someColumn`) FROM `collection`")
.then(res => res.json())

I logged the result It looks like this

  [ RowDataPacket {
         'SUM(`someColumn`)': 23231 }
  ]

wOR how do I get a directly total result?

Abhshek
  • 73
  • 1
  • 6
  • Possible duplicate of [NodeJS/Knex Creating Json Response](https://stackoverflow.com/questions/51557391/nodejs-knex-creating-json-response) – Milad Aghamohammadi Oct 28 '18 at 05:48
  • your logged result doesn't look like a valid javascript object, are you sure that's the output of `console.log(res)` ? – mihai Oct 29 '18 at 16:20

1 Answers1

0

Result returned by knex.raw() is some javascript object which depends on database dialect / driver that you are using. So you need to check from your database driver how it returns responses to queries.

If you use normal query methods, knex does automatically extract only an array of results from response got from driver. For MySQL extraction code is here: https://github.com/tgriesser/knex/blob/master/src/dialects/mysql/index.js#L139

Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70