title: NodeJS - Cannot set Headers after they are sent to the client
body: <p>So I've searched around and found out that to fix the said issue, I have to return after sending a response. But my problem is, even though I have return, I still have the error.</p>
  
  <pre><code>const dbEditCourse = (req, res, db, logger) =&gt; {
      let {
          origCourse, code, description, type
      } = req.body;
      if (!code || !description || !type) {
          res.json({
              haveEmpty: true
          });
          return;
      }
      db.transaction((trx) =&gt; {
              db.select('*').from('course_strand').where('code', '=', code)
                  .then(data =&gt; {
                      if (data[0]) {
  
                          //error happens in this block of code
                          res.json({
                              isSuccess: false
                          });
                          return;
                          //i also tried return res.json({ isSuccess: false });
  
                      }
                      //wrapping this in 'else' also does not work
                      return db('course_strand')
                          .returning('*')
                          .where('code', '=', origCourse)
                          .update({ code, description, type })
                  })
                  .then(course =&gt; {
                      return db('activity_logs')
                          .returning('*')
                          .insert({
                              date: new Date(),
                              employee_id: req.session.emp_id,
                              module: "COURSE / STRAND",
                              activity: "EDIT"
                          })
                  })
                  .then(activity =&gt; {
                      if (activity[0]) {
                          res.json({
                              isSuccess: true
                          });
                          return;
                      } else {
                          res.json({
                              isSuccess: false
                          });
                          return;
                      }
                  })
                  .then(trx.commit)
                  .catch(err =&gt; {
                      logger.error(err);
                      trx.rollback;
                      res.render('pages/error-500');
                  });
          })
          .catch(err =&gt; logger.error(err));
  }
  
  module.exports = {
      dbEditCourse
  }
  </code></pre>
  
  <p>What I'm doing to produce the error is, If the record is existing, it will go into the block of code above. Aside from that specific block of code, I don't encounter the error elsewhere. And the code is working fine even though I have the error.</p>