Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!