2017-02-15 69 views
0

我想在單個交易中包裝多個函數。雖然它沒有拋出任何錯誤,但交易沒有被提交。Bookshelfjs:交易中的多個功能

下面是樣本片段。

function doSomething(ids){ 
    bookshelf.transaction(function(trx){ 
     if(someCondition){ 
      new Service().save({ 'name': service.name },{transacting:trx}).then(function(){ 
       doSomeDBUpdate1(ids,trx); 
      }); 

     }else{ 
      doSomeDBUpdate1(ids,trx); 
     } 
    }) 
} 

function doSomeDBUpdate1(ids,trx){ 
    new accounts({ id: accountId }).services().attach(serviceIds,{transacting:trx}).then(function(){ 
    //do something 
    }) 
} 

回答

1

交易不知道你什麼時候完成了對它的新查詢......現在它從不提交。

這應該工作:

function doSomething(ids){ 
    bookshelf.transaction(function(trx){ 
    if(someCondition){ 
     return new Service() 
     .save({ 
      'name': service.name 
     }, {transacting:trx}) 
     .then(function(){ 
      return doSomeDBUpdate1(ids,trx); 
     }); 
    } else { 
     return doSomeDBUpdate1(ids,trx); 
    } 
    }) 
} 

function doSomeDBUpdate1(ids,trx){ 
    return new accounts({ id: accountId }) 
    .services() 
    .attach(serviceIds,{transacting:trx}) 
    .then(function() { 
     // do something... if async remember to return the promise 
    }); 
}