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

372
Visualizações
Request Timeout Issue - Node/Heroku

This post route below is timing out about 1/100 times. I am trying to optimize the code to avoid these timeout but they continue to happen. Is there anything I could do to further avoid these request timeouts?

I'm not sure how to troubleshoot this. This route shouldn't take over 30 seconds to post.

Thanks for any help!

router.post("/updateAccount", usernameToLowerCase, async function(req, res){
    
   
    if (req.user) {
    
    await User.findOne({username: req.body.currentUser}, function(err, user) {
    console.log('first ' + user)
    if (err) {
    console.log('cant find user err' + err)
    return done(err);
    }
    
    user.username = req.body.username;
    user.password = req.body.password;
    user.accountUpdated = true;
    
    
    user.save(function(err){
    
    if (err) {
    req.flash("error", "It looks like that email address is taken. Please use a different email address");
    res.redirect('back');
    
    } else {
    
    req.logout();
    
    req.login(user, function(err) {

    if (err) console.log('There was an account error' + err)
    req.flash("success", "Your account has been created! Your username is " + user.username );
    res.redirect('/results')
    
    });
    }
    });
    });
    
    }
    });
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

you can use findOneAndUpdate that finds a matching document, updates it according to the update arg

try{
    let filter = {username: req.body.currentUser};
    let user = {
        username = req.body.username;
        password = req.body.password;
        accountUpdated = true
    } 
    let user = await User.findOneAndUpdate(filter, user,{
        select: "_id username",
        new: true,
    });
    req.logout();
    req.login(user, function(err) {
        if (err) console.log('There was an account error' + err)
        req.flash("success", "Your account has been created! Your username is " + user.username );
        res.redirect('/results')
        });
}
catch(error){
    console.log(error)
    req.flash("error", "It looks like that email address is taken. Please use a different email address");
    res.redirect('back');
    //handle error
}

over 4 years ago · Santiago Trujillo Relatório

0

I believe since you are getting 1 out of 100 request timeout its more of a callback issue as the async event is in queue and no error is coming .

I also believe req.logout() being an async request must be handled before req.login().

Please check for these points before calculating it as a query issue.

over 4 years ago · Santiago Trujillo Relatório

0

Try to add more indexes to your User model, and use more than one field to find will help to increase the speed of the operation.

adding unique index in username filed will help

also you can use Model.updateOne() update the document without loading it from the database so the document is not in the Node.js process memory

if that does not help you can increase the timeout for the sever to any number you want

var server = app.listen()
server.timeout = 2000

but in your case you are talking about 30 sec. which is so much. try to use

try{}catch(){}

and see if any errors does not handle correctly

over 4 years ago · Santiago Trujillo 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