Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  32] [ 1]  / answers: 1 / hits: 21819  / 8 Years ago, sat, december 10, 2016, 12:00:00

I'm trying to use Knex with async/await since Knex has a Promise interface. My code is below.



const db = makeKnex({
client: 'mysql',
connection: {
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
},
pool: { min: 0, max: 100 },
});

async function getUsers() {
return await db.select()
.from('users')
.limit(10);
}
const res = getUsers();
console.log('KNEX', res);


I expected to get the rows of my query back, but the output is



KNEX Promise {
_c: [],
_a: undefined,
_s: 0,
_d: false,
_v: undefined,
_h: 0,
_n: false }

More From » mysql

 Answers
42

You should call await in a async signed function. Here is the pattern what I use.



(async function(){
const res = await getUsers();
console.log('KNEX', res);
})()

[#59752] Thursday, December 8, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daytonm

Total Points: 519
Total Questions: 83
Total Answers: 89

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;