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

129
Visualizações
Async Await inside for loop?

Im struggling with this. I have in my db Profile{name: string, table:0}, and im using a for loop to asign the number of the table to the different users, but this is not working. The for loop works fine if i checked in the console, but in the postman request, is not working.

When i filter by table number from the database, there are more than 5 results per table. And in somecases, there are irregular, like, table 1: 8 users, table 2: 5 users, so on...

const ProfileSchema = new Schema(
  {
    name: {
      type: String,
      required: true,
    },
    password: {
      type: String,
      required: true,
    },
    moderator: {
      type: Boolean,
      default: false,
    },
    email: {
      type: String,
      required: true,
    },
    country: {
      type: String,
      required: true,
    },
    institution: {
      type: String,
      /*  required: true */
    },
    score: {
      type: [Number],
      default: [],
    },
    active: {
      type: Boolean,
      required: true,
      default: false,
    },
    img: String,
    meetLink: String,
    table: {
      type: Number,
      default: 0,
    },
    group: Number,
  },

routes/index.js

router.put('/asignTable', async (req, res) => {
  let users = await Profile.find()
  shuffle(users)
  asignTable(users)
  res.send(users)

routes/utils.js

async function asignTable(users) {
  var contador = 0
  let numTable = 1
  filter = {}
  for (const user of users) {
    const filter = { name: user.name }
    const update = { table: numTable }
    if (contador == 5) {
      contador = 0
      numTable++
      console.log(`desde el if ${contador}`)
    } else if (contador < 5) {
      await Profile.findOneAndUpdate(filter, update).then(contador++)

      console.log(`desde el elseif  contador:${contador} numtable:${numTable}`)
    }
  }
}
})
about 4 years ago · Juan Pablo Isaza
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