2

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)

0

1 Answer 1

4

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');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.