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

244
Visualizações
How to make node.js function asynchronous? still returning empty object sometimes

I have an object in my Node.js route (result), and I want to split that into two separate objects and return them both. However, sometimes the second object is returned empty and to fix that I want to make the function that splits it asynchronous. For some reason its not working though, what am I doing wrong? The function is separateResult().

EDIT: The problem seems to be in my createResponse(rows) function. My SQL query always returns 6 rows but my createResponse(rows) function sometimes returns an object with 5 key-value pairs instead of 6.

function createResponse(rows) {
  var response = {}
  var random = 0
  for (let i = 0; i < 6; i++) {
    var random = Math.floor(Math.random() * 101)
    response[random] = rows[i].user_name
  }
  return response
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Because Math.random() sometimes generates same result. This can be any time 4/10, 3/5 ... anytime. Think of finding another way of giving key names to your response. E.G. you are using a loop. So i is always different. You can do response[rows[i].user_name+i]. Best practice would be using uuid library

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