sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

Sequelize Transactions and foreach

Open hfar opened this issue 11 years ago • 1 comments

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)
    });
});

hfar avatar Mar 15 '15 13:03 hfar

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

mickhansen avatar Mar 15 '15 13:03 mickhansen