Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda