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

370
Views
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 answers
Answer question

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 Report

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 Report

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