0

I just write two similar code like these, but only material can run and the order has the error "[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" when I submit the form

order.js

pool.getConnection(function(err,connection){
            connection.query('SELECT customerid FROM order_table WHERE customerid =?', [customerid],function(err,rows){
                if(err){
                    res.render('error',{
                        message:err.message,
                        error:err
                    });
                }
         /*
            if(rows.length>=1){
                res.render('operation/order',{warn:"You have already order!"});  

            }else{
                */   
                    var cmd="INSERT INTO order_table(customerid,customerName,phonenumber,productid,amount,orderday,deadlineday) VALUES(?,?,?,?,?,?,?)";
                        connection.query(cmd,[customerid,customerName,phonenumber,productid,amount,orderday,deadlineday],function(err,result){
                            console.log(req.body);
                            if(err){
                                res.redirect('/order');
                            //return next();
                            }else{
                                res.render('operation/order_redirect');
                            }
                    });
            //}
            });

        });
    //callback(err, result);
    connection.release();
});

module.exports=router;  
Surjeet Bhadauriya
  • 6,755
  • 3
  • 34
  • 52

2 Answers2

0

The error comes when you are trying send the response though it is already sent.

As I can see you have not returned the state when there is an error. So use return to return the state so that below code does not execute.

if(err){
   return res.render('error',{
         message:err.message,
         error:err
    });
}

Why this error occurs:


The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. (Find more)


Surjeet Bhadauriya
  • 6,755
  • 3
  • 34
  • 52
0

There's an error caught in you if block, but your if block doesn't have an else block, so the code followed by the if block is always executed whether an error occurred or whether not. So res.render is executed and a response is sent to the client, the rest of the code is being executed right after and when calling redirect headers are being set and you cannot do this after a response was already sent to the client.

Just add else to the rest of your code as follows:

pool.getConnection(function(err,connection){
            connection.query('SELECT customerid FROM order_table WHERE customerid =?', [customerid],function(err,rows){
                if(err){
                    res.render('error',{
                        message:err.message,
                        error:err
                    });
                }
                else {
                    var cmd="INSERT INTO order_table(customerid,customerName,phonenumber,productid,amount,orderday,deadlineday) VALUES(?,?,?,?,?,?,?)";
                        connection.query(cmd,[customerid,customerName,phonenumber,productid,amount,orderday,deadlineday],function(err,result){
                            console.log(req.body);
                            if(err){
                                res.redirect('/order');
                            //return next();
                            }else{
                                res.render('operation/order_redirect');
                            }
                    });
            }
            });

        });
    //callback(err, result);
    connection.release();
});

module.exports = router; 
Naor Levi
  • 1,713
  • 1
  • 13
  • 27