Skip to content
Advertisement

Electron, Javascript: How to select an array within an array returned by the database

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:

const { getConnection } = require('./database');

async function getUsers() {
   const conn = getConnection();
   var usuarios = (await conn).query('SELECT * FROM user');
   console.log(usuarios)
}     

But this variable comes this way:

enter image description here

And I’m not able to select that array (_rejectionHandler0)

Advertisement

Answer

Since .query also returns a Promise, you need to await it.

var usuarios = await (await conn).query('SELECT * FROM user');

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:

const conn = await getConnection();
var usuarios = await conn.query('SELECT * FROM user');
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement