如何使用nodejs在sequelize中使用事务

时间:2017-12-05 12:19:53

标签: node.js sequelize.js

代码抛出“TypeError:sequelize.transaction不是函数”错误

sequelize.transaction(transaction => {
          myDb.myTable.findAll({attributes:['IMAGEPATH'],where:{ID : customerId}})
          .then((result) => {

          ....

          }).then((commitResult) =>{
                //commit
          }).catch((rollbackErr) =>{
                //rollback  

          })

1 个答案:

答案 0 :(得分:2)

看起来您试图在定义上引用transaction函数而不是实例,即

const sequelize = require('sequelize');
sequelize.transaction(...); // doesn't exist

您需要创建一个续集实例并使用此

上的transaction函数
const Sequelize = require('sequelize');
const db = new Sequelize(...);
db.transaction(...); // works

请参阅docs