Search code examples
node.jsexpressreloadnodemon

Node.js stops listening (gets stuck) after executing query more than 6 times


I am using expressJs, This is the query which I am executing it multiple time

router.post('/Index_api/Reminder/Delete',function(req, res)
 {
      Reminder_Id = req.body; 
      DeleteRow('Reminders','Reminder_Id',Reminder_Id.Reminder_Id);

 });

int this is the DeleteRow Function

function DeleteRow(TableName,WCC, id)
 {

      var query ="Delete FROM `"+TableName + "` WHERE `"+ WCC +"` =" + id;

      conn.query(query,function(err,result)
       {
          if(err)
           {
               console.error(err);
               return;
           }else{
               console.log(result);
           }
      });
 }

I am posting data to this route like this:

function DeleteRow(id)
 {    
     $.post('/Index_api/Reminder/Delete',{
           Reminder_Id:id
     });
     $("#row"+id).remove();
 }

if I want to delete 6 records together there is no problem but by the 7th one is not executed and gets stuck. I am also using nodemon and reload package.


Solution

  • Your router.post() route handler is not returning any response. Thus, the browser is still waiting for some sort of response. The browser has a maximum number of connections it will make to any given host so once you have too many connections all sitting there waiting for a response, then the browser queues the next requests until one of the prior ones finishes.

    Eventually those requests will time out, but that can take a long time.

    To fix, just send a response from your route handler:

    router.post('/Index_api/Reminder/Delete',function(req, res) {
          Reminder_Id = req.body; 
          DeleteRow('Reminders','Reminder_Id',Reminder_Id.Reminder_Id);
          res.send("ok");
    });
    

    Or, if you want the post to actually respond after DeleteRow() is done, you can let DeleteRow() send the right response by passing res to it and letting it send the response.

    router.post('/Index_api/Reminder/Delete',function(req, res) {
          Reminder_Id = req.body; 
          DeleteRow('Reminders','Reminder_Id',Reminder_Id.Reminder_Id, res);
    });
    
    function DeleteRow(TableName, WCC, id, res) {
        var query = "Delete FROM `" + TableName + "` WHERE `" + WCC + "` =" + id;
    
        conn.query(query, function(err, result) {
            if (err) {
                console.error(err);
                res.status(500).send("delete failed");
            } else {
                res.send("delete OK");
            }
        });
    }
    

    Then, you should probably also change your client code to actually look at the returned status and act accordingly.

    function DeleteRow(id) {    
         $.post('/Index_api/Reminder/Delete',{
               Reminder_Id:id
         }).then(function(result) {
             if (result.status == 200) {
                 // successful
                 $("#row"+id).remove();
             } else {
                 // handle server error here
             }
         }, function(err) {
               // network error here
         });
    }