2016-06-30 52 views
1

我在节点执行这个膝盖查询:节点Knex插入不执行

return Knex.transaction(function (tx) { 
     debug("Inserting new story record"); 
     return tx.insert({ 
      'projectId':projectId, 
      'title': title, 
      'story': text, 
      'points': 0, 
      'storyNumber': Knex('story').max('storyNumber').where('projectId', projectId) 
     }, 'id') 
      .into('story') 
      .then(function (id) { 
       debug("Returning story for %s", id); 
       return getStory(id); 
      }) 
    }) 

但“则()函数永远不会被调用。有人知道为什么

我一直在阅读所有的knex doco,看起来我做的一切都是正确的。该命令的调试如下所示:

crux:db Inserting new story record +4ms 
{ method: 'insert', 
    options: {}, 
    timeout: false, 
    cancelOnTimeout: false, 
    bindings: [ 0, 2, 'test', 2, 'title' ], 
    __knexQueryUid: 'aa5ff1d3-eff0-4687-864b-772c26e1aebd', 
    sql: 'insert into `story` (`points`, `projectId`, `story`, `storyNumber`, `title`) values (?, ?, ?, (select max(`storyNumber`) from `story` where `projectId` = ?), ?)' } 

所以这对我来说都很好。永远不要执行。

回答

0

需要更多信息......您可能从未触发过交易。在调用.then或试图在promise链中解析事务之前不会执行事务(QueryBuilder和Transaction是Promise A + spec调用thenableshttps://promisesaplus.com 1.2的东西)。

其他可能性是插入引发一个错误,所有的东西都回滚,然后永远不会被调用。

试试这个它可以处理一些错误情况,并可以帮助你找出真正的原因:

return Knex.transaction(function (tx) { 
    debug("Inserting new story record"); 
    return tx.insert({ 
    'projectId':projectId, 
    'title': title, 
    'story': text, 
    'points': 0, 
    'storyNumber': Knex('story').max('storyNumber').where('projectId', projectId) 
    }) 
    .into('story') 
    .then(function (id) { 
    debug("Returning story for %s", id); 
    return getStory(id); 
    }) 
    .catch(function (err) { 
    debug("Insert failed", err); 
    throw err; 
    }) 
}) 
// just to make sure that transaction is triggered (in your code caller is responsible of that) 
.then(function (blah) { return blah; }); 
.catch(function (err) { debug("Huh! Transaction failed!", err); throw err; }); 

而且在.insert({...},返回),你似乎使用mysql这没有按”支持传递返回的参数。所以我放弃了我的建议...