Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
How to do many SQL queries in a for loop with Nodejs and Express

I have a req.body from a Post form with many values like this

console.log(req.body)

{

productid:[1, 2, 3]
qty: [3, 5, 6]

}

I want to make a for loop and use the different values in a SQL query But I get stuck on the first query problaby because its an async await function.

My question is how can I do many sql queries with a forloop and still use an async await function?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can do it like this:

for(let item of array) {
   let res = await query(item)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Since I only do an update and not waiting for an answer I didnt need to use await async. It worked when I remove async/await.

Thank you

Sorry for not so clear question

about 4 years ago · Juan Pablo Isaza Relatório

0

if you want to do them in parallel you can create an array of promises and use Promise.all to wait for all the operations to finish, in this way you can optimize you code a lot instead of doing in a sequential way.

    const operations = []
    for(let item of array) {
      operations.push(query(item))
    }
    const result = await Promise.all(operations)
    //Do wherever you want with your results.

In order to execute your code, your function which handle the request must be declared with the async keyword.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda