I want to foreach an array to show on the screen. This array is the data from a database table.
The code to get this data:
JavaScript
x
8
1
const { getConnection } = require('./database');
2
3
async function getUsers() {
4
const conn = getConnection();
5
var usuarios = (await conn).query('SELECT * FROM user');
6
console.log(usuarios)
7
}
8
But this variable comes this way:
And I’m not able to select that array (_rejectionHandler0)
Advertisement
Answer
Since .query
also returns a Promise
, you need to await
it.
JavaScript
1
2
1
var usuarios = await (await conn).query('SELECT * FROM user');
2
await conn
waits for the connection to be obtained and the outer await
waits for the query to finish.
Alternatively, you can await
each one separately:
JavaScript
1
3
1
const conn = await getConnection();
2
var usuarios = await conn.query('SELECT * FROM user');
3