title: sharing a transaction or task object between multiple queries across multiple controllers in pg-promise
body: <p>I am relatively new to node.js, postgresql, promises and infact stackoverflow so apologies in advance if things sound a little disjointed!</p>
  
  <p>I am currently trying to run multiple queries within chained promises spread across various controllers.  I want to run all the queries within the same transaction or task to eliminate multiple connects and disconnects to the database.  </p>
  
  <p>I have tried the following where I am adding a student and assigning two mentors for that student.  The HTTP request is routed to the student controller which adds a student via the student repository.  The student repository is where the task starts and is returned to the controller which forwards it to the mentor controller and along the chain it goes...</p>
  
  <pre><code>@ HttpPost("/api/students/create")
  addStudent( @ Req()request) {
  
  var studentid;
  var mentorids= [];
  //Add the student
  return this.studentRepository.addStudent(request.body.student)
  .then(newStudentId =&gt; {
      studentid = newStudentId;
  
      //Add the mentors, passing the transaction object received back from
        studentRepository
  
      return this.mentorController.addMentor(request.body.schoolid, request.body.mentors, newStudentId.transaction)
      .then(result =&gt; {
          var data = [];
          console.log(result);
          for (var role in result) {
              data.push({
                  mentorid: result[role].roleid,
                  studentid: studentid
              });
          }
          //Assigns the student to mentors in the link table
          return this.studentRepository.assignMentor(data)
          .then(result =&gt; {
              return result;
          })
  
      })
  
  });
  
  }
  </code></pre>
  
  <p>Student repository</p>
  
  <pre><code>addStudent(student): any {
  return this.collection.task(t =&gt; {
      return this.collection.one(this.sql.addStudent, student)
      .then(studentid =&gt; {
          return {
              studentid: studentid.studentid,
              transaction: t
          }
      });
  
  })
  }
  </code></pre>
  
  <p>Mentor controller</p>
  
  <pre><code>addMentor(institutionid: number, mentors, t): any {
  
  var promises = [];
  var mentorIds = [];
  for (var role in mentors) {
      promises.push(this.roleController.registerRole(institutionid,mentors[role].role,t));
  }
  return t.batch(promises)
  .then(result =&gt; {
      return Promise.resolve(result);
  })
  
  }
  </code></pre>
  
  <p>Role controller</p>
  
  <pre><code>@ HttpPost("/api/roles/register")
  registerRole(institutionid,  @ Req()request, t ? ) : any {
  console.log(request);
  return this.roleRepository.checkRoleEnrollment(institutionid, request.email, request.roletype, t)
  .then(result =&gt; {
      return this.roleRepository.addRoleEnrollment(institutionid, request, t)
      .then(data =&gt; {
          return this.roleRepository.updateRoleEnrollment(data.roleenrollmentid, data.roleid)
          .then(d =&gt; {
              return data;
          })
  
      })
  })
  .catch (error =&gt; {
      return Promise.reject(error);
  });
  }
  </code></pre>
  
  <p>I am getting the following error when I call checkEnrollment in the Role Controller:</p>
  
  <pre><code>"name": "Error",
  "message": "Unexpected call outside of task.",
  "stack": "Error: Unexpected call outside of task. at Task.query   
  (\api\node_modules\pg-promise\lib\task.js:118:19) 
  at Task.obj.oneOrNone (\api\node_modules\pg-promise\lib\database.js:491:31)         
  at RoleRepository.checkRoleEnrollment.... 
  </code></pre>
  
  <p>Any help would be much appreciated.  Thanking you in advance.</p>