【问题标题】:Node Knex insert not executing节点 Knex 插入未执行
【发布时间】:2016-07-01 19:26:53
【问题描述】:

我正在节点中执行这个膝盖查询:

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()' 函数。有人知道为什么吗?

我一直在阅读所有 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` = ?), ?)' }

所以我觉得这一切都很好。只是从不执行。

【问题讨论】:

    标签: node.js knex.js


    【解决方案1】:

    需要更多信息...您可能只是从未触发交易。在调用.then 或尝试在promise 链中解析之前,不会执行事务(QueryBuilder 和Transaction 是Promise A+ 规范调用thenableshttps://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({...}, returned) 中,您似乎使用了不支持传递返回参数的 mysql。所以我把它从我的建议中删除了......

    【讨论】:

      猜你喜欢
      • 2018-05-10
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      相关资源
      最近更新 更多