sequelize
sequelize copied to clipboard
Sequelize Transactions and foreach
Hello,
I am using sequelize latest version and i am trying to use sequelize managed transaction,when i use forEach inside the transaction it commits the changes in case of errors instead of Rollback,maybe i am doing something wrong below but i can't figure out the problem, could you please help.
My test case is : when Third block through an error ,the things done in First and Second block get committed.
return sequelize.transaction(function (t) {
//First Block
return db.VenueTag.destroy(...
}, {transaction: t}).then(function () {
//Second Block
req.body.Tags.forEach (function (e) {
return db.VenueTag.upsert({...});
});
}, {transaction: t}).then(function () {
//Third Block
return venue.updateAttributes({...},{ transaction: t});
});
}).then(function (result) {
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback
return res.status(200).json(result);
}).catch(function (err) {
// Transaction has been rolled back
// err is whatever rejected the promise chain returned to the transaction callback
return res.status(500).send({
message: errorHandler.getErrorMessage(err)
});
});
You are not returning any promise, hence breaking the promise chain, take a look at https://github.com/petkaantonov/bluebird/blob/master/API.md#mapfunction-mapper--object-options---promise