Request Timeout Issue - Node/Heroku

Viewed 198

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')
    
    });
    }
    });
    });
    
    }
    });
3 Answers

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
}

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.

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

Related