How to access knex query results
I'd suggest using async/await functions. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
router.get('/robots', async function (req, res) {
const bots = await knex.select('id', 'name').from('robots');
res.render('robots/index', {
page_title: 'All Robots',
robots: bots
}); // res.render
}); // router.get
knex
uses Promises. Specifically, it uses http://bluebirdjs.com/docs/getting-started.html. console.log(bots)
will not work because it is called right away, while .then( ... )
is only called after the knex
query has been successfully called and run.
Your edited, "synchronous workaround" is the correct method to run the Express response, though you do not need to set that query to var response
(see my comment on your question for more on that).