Sindre
Sindre

Reputation: 140

How I can translate this very unsafe query to a Knex querybuilder call?

I started with a query call like this:

const query = "SELECT MAX(timeStamp), Temp FROM dataTable WHERE sensorid = " + req.params.id + ";"

After using the docs and cheat sheet, I have something like this:

const query = knex("dataTable").where({'sensorid': req.params.id}).max('timeStamp').first()

But when I console.log(query) I am getting stuff that I wasn't expecting

Upvotes: 0

Views: 132

Answers (1)

felixmosh
felixmosh

Reputation: 35573

Knex returns a Promise, so you need to await it.

It should look like this:

const query = await knex('dataTable')
  .max('timeStamp')
  .where({ sensorid: req.params.id })
  .first();

Upvotes: 1

Related Questions