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

128
Views
await in javascript for of loop using mongoose

is there anything wrong with my code? i use express and mongoose

router.get('/c/:hashtoken', validateEmailToken, catchAsync(async(req,res)=>{
    const hashtoken = req.params.hashtoken
    const hashtoken2 = createHash('sha256').update(hashtoken).digest('hex')
    const realHashTokens = await EmailToken.find({hashtoken2})
    if(realHashTokens.length===0){
        req.flash('error','Incorrect email verification token or Your account has been activated')
        return res.redirect('/')
    }
    const toDeleteTokenId = []
    for(let realHashToken of realHashTokens){
        let theUser = await User.findById(realHashToken.user)
        theUser.active = true
        await theUser.save()
        toDeleteTokenId.push(realHashToken._id)
    }
    for (let theId of toDeleteTokenId){
        await EmailToken.findByIdAndDelete(theId)
    }
    req.flash('success','Success! Your account has been activated')
    return res.redirect('/')
}))

the page has never responded. it keeps loading, but user becomes active if i checked in the database

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Set an error handler or wrap in try/catch. Also add more debug statments.

router.get('/c/:hashtoken', validateEmailToken, catchAsync(async(req,res)=>{
try {
    console.log('request received')
    const hashtoken = req.params.hashtoken
    const hashtoken2 = createHash('sha256').update(hashtoken).digest('hex')
    console.log('finding token')
    const realHashTokens = await EmailToken.find({hashtoken2})
    if(realHashTokens.length===0){
        req.flash('error','Incorrect email verification token or Your account has been activated')
        return res.redirect('/')
    }
    const toDeleteTokenId = []
    console.log('finding user')
    for(let realHashToken of realHashTokens){
        let theUser = await User.findById(realHashToken.user)
        theUser.active = true
        await theUser.save()
        toDeleteTokenId.push(realHashToken._id)
    }
    console.log('deleting')
    for (let theId of toDeleteTokenId){
        await EmailToken.findByIdAndDelete(theId)
    }
    console.log('completed - redirecting')
    req.flash('success','Success! Your account has been activated')
    return res.redirect('/')
}
 catch(e) {
   console.error(e)
   // res.sendStatus(500)
   req.flash('error', e.message)
   return res.redirect('/')
}
}))

about 4 years ago · Juan Pablo Isaza Report

0

I have solved the problem. the User model in this code has post mongoose middleware and within that function, I call next() even though there's no other post middleware after that so the server responds with long responds and the page keeps loading.

about 4 years ago · Juan Pablo Isaza Report
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!