title: "Can't set headers after they are sent" Error (Even with "Return" Added)
body: <p>I'm an amateur developer working on a case management system for a capstone project in school. I'm using node.js/express in the backend. Every time I try to execute the following function, which saves all the case data from a very long form, I get the error "can't set headers after they are sent to the client". </p>
  
  <p>There are multiple conditional statements in the same function because the requirements for saving some of the data is dependent on the user's input. </p>
  
  <p>I've read everything I could about the issue on forums here and most advise adding "return" statements to the responses being sent within the conditional statements. But I've done that, and I still get the same error. I even tried adding "res.end" within the conditional statements to make sure the code within the entire function runs seamlessly.</p>
  
  <p>I know there is some key concept that I'm missing or do not understand well, so I'm hoping someone can enlighten me. Please help.</p>
  
  <p>Thanks. </p>
  
  <pre><code>saveCaseData: (req, res) =&gt;{
  
          let caseID = req.body.caseID;
          let clientID = req.body.clientID;
  
          let firstName = req.body.firstName;
          let lastName = req.body.lastName;
  
          //clientcase table
          let date_intake = req.body.date_intake;
          let referred_by = req.body.referred_by;
          let contactInitiation = req.body.contactInitiation; 
          let client_electronic_signature = req.body.client_electronic_signature;
          let contact_electronic_signature = req.body.contact_electronic_signature;
          let dcm_electronic_signature = req.body.dcm_electronic_signature;
  
          //client table
          let gender = req.body.gender;
          let race = req.body.race;
          let ethnicity = req.body.ethnicity;
          let marital_status = req.body.marital_status;
  
          let best_contact_time = req.body.best_contact_time;
          let alternate_phone = req.body.alternate_phone;
          let preferred_language = req.body.preferred_language;
          let literateEnglish = req.body.literateEnglish;
          let literate_preferredLanguage = req.body.literate_preferredLanguage;
  
          let doesNotSpeakEnglish = req.body.does_not; 
  
          let primary_phone = req.body.primary_phone; 
          let email = req.body.email; 
  
           //emergency_contact table
           let emergencyContactFName = req.body.emergencyContactFName;
           let emergencyContactLName = req.body.emergencyContactLName;
           let relationship_to_applicant = req.body.relationship_to_applicant;
           let proxy = req.body.proxy; 
  
  
           //Risks
          let risk1 = req.body.r1;
          let risk2 = req.body.r2;
          let risk3 = req.body.r3;
          let risk4 = req.body.r4;
          let risk5 = req.body.r5;
  
          let risk8 = req.body.r8;
          let risk9 = req.body.r9;
          let risk10 = req.body.r10;
          let risk11 = req.body.r11;
          let risk12 = req.body.r12;
          let risk13 = req.body.r13;
          let risk14 = req.body.r14;
          let risk15 = req.body.r15;
          let risk16 = req.body.r16;
          let risk17 = req.body.r17;
          let risk18 = req.body.r18;
          let risk19 = req.body.r19;
          let risk20 = req.body.r20;
          let risk21 = req.body.r21;
          let risk22 = req.body.r22;
          let risk23 = req.body.r23;
          let risk24 = req.body.r24;
          let risk25 = req.body.r25;
          let risk26 = req.body.r26;
          let risk27 = req.body.r27;
  
  
          //clientcase table
  
          let query1 = "UPDATE `clientcase` SET `date_intake`='"+date_intake+"', `referred_by`='" + referred_by + "', `contact_initiation`='"+contactInitiation+"', `client_electronic_signature`='" + client_electronic_signature + "', `contact_electronic_signature`='" + contact_electronic_signature + "', `dcm_electronic_signarture`='" + dcm_electronic_signature + "' WHERE `case_id`='"+caseID+"';";
  
          db.query(query1, (err, result, fields) =&gt; {
              if (err) {
                  return res.status(500).send(err);
              }
              console.log("Case Query Success - clientcase data saved.");
              res.end(); 
          });
  
  
          //client table
  
          let clientQuery1 = "UPDATE `client` SET `gender`='"+gender+"', `race`='"+race+"', `ethnicity`='"+ethnicity+"',`marital_status`='"+marital_status+"', `best_contact_time`='"+best_contact_time+"', `alternate_phone`='"+alternate_phone+"', `preferred_lanugage`='"+preferred_language+"', `reads_writes_English`='"+literateEnglish+"', `reads_writes_preferred_language`='"+literate_preferredLanguage+"' WHERE `case_id`='"+caseID+"';";
  
          db.query(clientQuery1, (err, result, fields) =&gt; {
              if (err) {
                  return res.status(500).send(err);
              }
              console.log("Client Query 1/3 Success - client data saved.");
              res.end();
          });
  
          if(doesNotSpeakEnglish){
              let clientQuery2 = "UPDATE `client` SET `speaks_English`='no' WHERE `case_id`='"+caseID+"';";
              db.query(clientQuery2, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Client Query 2/3 - No, client doesn't speak English saved.");
                  res.end();
              });
  
          } else {
              let clientQuery3 = "UPDATE `client` SET `speaks_English`='yes' WHERE `case_id`='"+caseID+"';";
              db.query(clientQuery3, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Client Query 3/3 - Yes, client speaks English saved.");
                  res.end();
              });
  
          }
  
          //emergency_contact table
  
          let contactQuery1 = "UPDATE `emergency_contact` SET `first_name`='"+emergencyContactFName+"', `last_name`='"+emergencyContactLName+"', `relationship_to_applicant`='"+relationship_to_applicant+"', `proxy`='"+proxy+"' WHERE `case_id`='"+caseID+"';";
  
          db.query(contactQuery1, (err, result, fields) =&gt; {
              if (err) {
                  return res.status(500).send(err);
              }
              console.log("Emergency Contact Query Sucess - Case data saved");
              res.end();
          });
  
          //Assigning Priority Level and Saving to the DB
  
          if((risk15 &amp;&amp; risk16) &amp;&amp; (risk1 || risk2 || risk3 || risk4 || risk5 || risk8 || risk9 || risk10|| risk11 || risk12 || risk13 || risk14 || risk17 || risk18)){
              let priorityQuery = "UPDATE `clientcase` SET `priority_level` = 'P4' WHERE `clientcase`.`case_id` = '" + caseID +"' ";
              db.query(priorityQuery, (err, result, fields) =&gt; {
                          if (err) {
                              return res.status(500).send(err);
                          }
                          console.log("Priority-4 Level Updated");
                          res.end();
                      });
          }
  
  
         else if(risk1 || risk2 || risk3 || risk4 || risk5 || risk8 || risk9 || risk10|| risk11 || risk12 || risk13 || risk14 || risk15 || risk16 || risk17 || risk18){
              let priorityQuery = "UPDATE `clientcase` SET `priority_level` = 'P3' WHERE `clientcase`.`case_id` = '" + caseID +"' ";
              db.query(priorityQuery, (err, result, fields) =&gt; {
                          if (err) {
                              return res.status(500).send(err);
                          }
                          console.log("Priority-3 Level Updated");
                          res.end();
                      });
          }
  
          else if(risk23 || risk19 || risk20 || risk21 || risk22 || risk24 || risk25 || risk26 || risk27){
              let priorityQuery = "UPDATE `clientcase` SET `priority_level` = 'P2' WHERE `clientcase`.`case_id` = '" + caseID +"' ";
              db.query(priorityQuery, (err, result, fields) =&gt; {
                          if (err) {
                              return res.status(500).send(err);
                          }
                          console.log("Priority-2 Level Updated");
                          res.end();
                      });
          }
  
          else{
              let priorityQuery = "UPDATE `clientcase` SET `priority_level` = 'P1' WHERE `clientcase`.`case_id` = '" + caseID +"' ";
              db.query(priorityQuery, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Priority-1 Level Updated");
                  res.end();
              });
          }
  
          //Save Risks to Case Record in the Database (consider switch)
  
          if (risk1) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '1')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 1 added to case record");
                  res.end();
              });
  
          }
  
          if (risk2) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '2')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 2 added to case record");
                  res.end();
              });
  
          }
  
          if (risk3) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '3')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 3 added to case record");
                  res.end();
              });
  
          }
  
          if (risk4) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '4')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 4 added to case record");
                  res.end();
              });
  
          }
  
          if (risk5) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '5')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 5 added to case record");
                  res.end();
              });
  
          }
  
          if (risk8) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '8')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 8 added to case record");
                  res.end();
              });
  
          }
  
          if (risk9) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '9')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 9 added to case record");
                  res.end();
              });
  
          }
  
          if (risk10) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '10')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 10 added to case record");
                  res.end();
              });
  
          }
  
          if (risk11){
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '11')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 11 added to case record");
                  res.end();
              });
  
          }
  
          if (risk12) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '12')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 12 added to case record");
                  res.end();
              });
  
          }
  
          if (risk13) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '13')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 13 added to case record");
                  res.end();
              });
  
          }
  
          if (risk14) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '14')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 14 added to case record");
                  res.end();
              });
  
          }
  
          if (risk15) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '15')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 15 added to case record");
                  res.end();
              });
  
          }
  
          if (risk16) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '16')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 16 added to case record");
                  res.end();
              });
  
          }
  
          if (risk17) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '17')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 17 added to case record");
                  res.end();
              });
  
          }
  
          if (risk18) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '18')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 18 added to case record");
                  res.end();
              });
  
          }
  
          if (risk19) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '19')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 19 added to case record");
                  res.end();
              });
  
          }
  
          if (risk20) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '20')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 20 added to case record");
                  res.end();
              });
  
          }
  
          if (risk21) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '21')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 21 added to case record");
                  res.end();
              });
  
          }
  
          if (risk22) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '22')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 22 added to case record");
                  res.end();
              });
  
          }
  
          if (risk23) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '23')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 23 added to case record");
                  res.end();
              });
  
          }
  
          if (risk24) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '24')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 24 added to case record");
                  res.end();
              });
  
          }
  
          if (risk25) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '25')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 25 added to case record");
                  res.end();
              });
  
          }
  
          if (risk26) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '26')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 26 added to case record");
                  res.end();
              });
  
          }
  
          if (risk27) {
  
              let query = "INSERT INTO `case_risk` (case_id, risk_id) VALUES ('" + caseID + "', '27')"; 
  
              db.query(query, (err, result, fields) =&gt; {
                  if (err) {
                      return res.status(500).send(err);
                  }
                  console.log("Risk 27 added to case record");
                  res.end();
              });
  
          }
  
         res.redirect("/dcm-home/recommendedPrograms/" + caseID + " ");
  
      },
  </code></pre>