关于knex.js的事务使用
发布于 8 年前 作者 BrotherPeng 9831 次浏览 来自 问答

knex.transaction(function(trx) {

            knex.transacting(trx).select('*').from('lottery_num').where('gid', '=', order.goodsid).limit(order.count).offset(0)//查询抽奖号码

                .then(function(data){
                    //data = data.join(',');
                    var lottery_nums = [];
                    var lids = [];

                    for(var i=0;i<data.length;i++){
                        lottery_nums.push(data[i].lottery_num);
                        lids.push(data[i].id);
                    }
                    debug('~~~~~~~~~~~~~~~~~');
                    debug(lids);
                    debug(lottery_nums);
                    debug('~~~~~~~~~~~~~~~~~');
                    order.lottery_num = lottery_nums.join(',');
                    return knex('lottery_num').transacting(trx).whereIn('id', lids)
                        .del()
                        .then(function(del){
                            return knex.transacting(trx).insert(order, 'id')
                             **   .into('joininfo');**
                        })

                }).then(trx.commit)
                .catch(trx.rollback);

        }).then(function(inserts) {
            debug(inserts.length + ' new books saved.');
            callback('success');
            })
            .catch(function(error) {
                // If we get here, that means that neither the 'Old Books' catalogues insert,
                // nor any of the books inserts will have taken place.
                debug(error);
                callback('fail');
            });
			
			
			写了这段代码,当into('joininfo');这个地方出现了异常的时候del()直接把数据删掉了,不知道回滚出现了什么问题,有使用过的吗,帮忙看下,不太懂promise
回到顶部