How to access a query return

Asked

Viewed 18 times

0

I’m trying to add values using push, turns out it’s not working...

Only works inside the code block connection.query().

Is there any way to access variables and add values to them outside this scope?

My current code:

let customers = []

connection.query("SELECT * FROM customers", async (error, data) => {
  customers.push(data) // console.log(customers) aqui funciona
})

console.log(customers) // retorno => [] (vazio)
  • 1

    Have you tried using the await in the conection.query? probably the.log console is running before the customers.push(data)

  • 1

    In addition to the link above see also: https://answall.com/q/264291/112052

  • Yes, I tried even I would put the code here, but as it was the same return []I didn’t put

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.