Manimaran
Manimaran

Reputation: 405

how to use transaction in sequelize using nodejs

Below code throw "TypeError: sequelize.transaction is not a function" Error

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

          ....

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

          })

Upvotes: 5

Views: 5298

Answers (1)

James
James

Reputation: 82136

It looks like you're trying to reference the transaction function on the definition as opposed to the instance i.e.

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

You need to create a sequelize instance and use the transaction function on this

const Sequelize = require('sequelize');
const db = new Sequelize(...);
db.transaction(...); // works

See the docs.

Upvotes: 10

Related Questions