续集更新事务

时间:2016-04-07 07:57:37

标签: node.js transactions sequelize.js

我在Nodejs中使用sequalize事务,但我的问题是它不会在Transaction中取我的users表并更新我的表

return sequelize.transaction(function (t) {
    var Users = objAllTables.users.users();
    return Users.update(updateUser, {
        where: {
            uid: sessionUser.uid,
            status: 'ACTIVE'
        }
    },{ transaction: t }).then(function (result) {

       return Utils.sendVerificationEmail(sessionUser.uid, sessionUser.user_email)
            .then(function(data){
                 data = false;  
                if(data == false){
                        throw new Error('Failed Email');
                }

            });


    }).then(function (result) {
        console.log(result);
        // Transaction has been committed
        // result is whatever the result of the promise chain returned to the transaction callback
    })

}).catch(function(err){
    res.send({message:err.message})
})

CONSOLE:

Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): START TRANSACTION;
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): SET autocommit = 1;
Executing (default): UPDATE `users` SET `username`='edited' WHERE `uid` = 20 AND `status` = 'ACTIVE'
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): ROLLBACK;

正如您在控制台更新查询中看到的那样,用完了事务

1 个答案:

答案 0 :(得分:25)

transaction密钥必须位于options

return Users.update(updateUser, {
        where: {
            uid: sessionUser.uid,
            status: 'ACTIVE'
        },
        transaction: t     //second parameter is "options", so transaction must be in it
    })