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

但是'then()'函数永远不会被调用。有人知道为什么吗?

我已经看了所有的文档,看起来我做的一切都是对的。该命令的调试如下所示:

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` = ?), ?)' }

所以在我看来一切都很好。只是永远不执行。

需要更多的信息…您可能从来没有触发过事务。事务将不会执行,直到.then被调用或它试图在承诺链中解决(QueryBuilder和事务是承诺A+规范调用thenables https://promisesaplus.com 1.2)。

另一种可能性是insert抛出错误,所有内容都被回滚,并且.then将永远不会被调用。

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

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不支持传递返回参数。

最新更新