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

134
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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