在pg-promise

时间:2017-01-18 14:51:26

标签: node.js postgresql transactions controllers pg-promise

我对node.js,postgresql,promises和infact stackoverflow相对较新,如果事情听起来有点脱节,请提前道歉!

我目前正在尝试在各种控制器中传播的链式承诺中运行多个查询。我想在同一个事务或任务中运行所有查询,以消除多个连接并断开与数据库的连接。

我尝试了以下内容,我正在为该学生添加一名学生并指派两名导师。 HTTP请求被路由到学生控制器,学生控制器通过学生存储库添加学生。学生存储库是任务开始的地方,并返回给控制器,控制器将其转发给指导者控制器,并沿着链路进行...

@ HttpPost("/api/students/create")
addStudent( @ Req()request) {

var studentid;
var mentorids= [];
//Add the student
return this.studentRepository.addStudent(request.body.student)
.then(newStudentId => {
    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 => {
        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 => {
            return result;
        })

    })

});

}

学生资料库

addStudent(student): any {
return this.collection.task(t => {
    return this.collection.one(this.sql.addStudent, student)
    .then(studentid => {
        return {
            studentid: studentid.studentid,
            transaction: t
        }
    });

})
}

导师控制器

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 => {
    return Promise.resolve(result);
})

}

角色控制器

@ 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 => {
    return this.roleRepository.addRoleEnrollment(institutionid, request, t)
    .then(data => {
        return this.roleRepository.updateRoleEnrollment(data.roleenrollmentid, data.roleid)
        .then(d => {
            return data;
        })

    })
})
.catch (error => {
    return Promise.reject(error);
});
}

当我在角色控制器中调用checkEnrollment时,我收到以下错误:

"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.... 

非常感谢任何帮助。提前感谢你。

1 个答案:

答案 0 :(得分:3)

根据我之前的评论:

  

该错误意味着您正在尝试访问由任务的回调函数之外的任务分配的连接t,即任务的回调已返回,连接已释放,并且那么你正在使用来自其他地方的任务分配的连接对象,当然这是无效的。

b.t.w。我是pg-promise;)

的作者

以下是您的代码以简化形式有效执行的操作:

var cnReference;

db.task(t => {
    cnReference = t;

    // can only use `t` connection while executing the callback
})
    .then(data => {
        // we are now outside of the task;
        // the task's connection has been closed already,
        // and we can do nothing with it anymore!
        return cnReference.query('SELECT...');
    })
    .catch(error => {
        // ERROR: Unexpected call outside of task.

        // We cannot use a task connection object outside of the task's callback!
    });

您需要更正实施,以确保不会发生这种情况。