javascript - knex.js - 读取然后更新会引发错误

标签 javascript node.js sqlite knex.js

我正在尝试链接读取和更新操作。首先,我根据 id 从数据库中读取(获取)数据,然后更改它并希望更新数据库中的数据,但收到 未处理的拒绝错误:当前客户端上没有定义池 错误。

index.js

var id = args.id;
var proj = new Project(id);

proj.read(knex).then(function(rows) {
  proj.set(args);
  proj.update(knex).then(console.log); // <== ERROR THROWN HERE
});

Project.js(仅限相关函数)

read(knex) {
  var self = this;

  return knex('projects').where('id', this.id).then(function(rows) {
    if (rows.length == 0)
      throw '[panda] Read project: no project with id = ' + self.id;
    self.set(rows[0]);

    return self;
  });
}

update(knex) {
  console.log(knex('projects').where('id', this.id).update(this).toString());

  return knex('projects').where('id', this.id).update(this);
}

命令

$ node index.js projects update --id=5 --name=Test-update
update "projects" set "completion" = 0, "currentIteration" = NULL, "id" = 5, "name" = 'Test-update' where "id" = 5
Unhandled rejection Error: There is no pool defined on the current client
    at /home/flawyte/development/projects/js/panda/node_modules/knex/lib/client.js:202:25
    at tryCatcher (/home/flawyte/development/projects/js/panda/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._resolveFromResolver (/home/flawyte/development/projects/js/panda/node_modules/bluebird/js/main/promise.js:480:31)
    at new Promise (/home/flawyte/development/projects/js/panda/node_modules/bluebird/js/main/promise.js:70:37)
    at Client.acquireConnection (/home/flawyte/development/projects/js/panda/node_modules/knex/lib/client.js:200:12)
    at /home/flawyte/development/projects/js/panda/node_modules/knex/lib/runner.js:136:49
    at tryCatcher (/home/flawyte/development/projects/js/panda/node_modules/bluebird/js/main/util.js:26:23)
    at Function.Promise.attempt.Promise.try (/home/flawyte/development/projects/js/panda/node_modules/bluebird/js/main/method.js:31:24)
    at Runner.ensureConnection (/home/flawyte/development/projects/js/panda/node_modules/knex/lib/runner.js:135:26)
    at Runner.run (/home/flawyte/development/projects/js/panda/node_modules/knex/lib/runner.js:30:31)
    at QueryBuilder.Target.then (/home/flawyte/development/projects/js/panda/node_modules/knex/lib/interface.js:27:43)
    at /home/flawyte/development/projects/js/panda/index.js:80:31
    at tryCatcher (/home/flawyte/development/projects/js/panda/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (/home/flawyte/development/projects/js/panda/node_modules/bluebird/js/main/promise.js:507:31)
    at Promise._settlePromiseAt (/home/flawyte/development/projects/js/panda/node_modules/bluebird/js/main/promise.js:581:18)
    at Promise._settlePromises (/home/flawyte/development/projects/js/panda/node_modules/bluebird/js/main/promise.js:697:14)

如果我删除 index.js 中的 then(console.log) ,则不会引发错误,但脚本结束后数据库中的数据保持不变。为什么?

最佳答案

我的脚本末尾有一行 knex.destroy(); ,由于 knex 是异步的,因此该行在 read() 指令之后执行(由于执行速度快),但在 update()之前执行,从而导致上述错误。

Project.js

var done = false;

proj.read(knex).then(function(rows) {
  proj.set(args);
  proj.update(knex).then(function(res) {
    done = true;
    console.log(res);
    knex.destroy();
  });
});

// ...

if (!done)
  // End database connection here
  // in case an error prevented it from being destroyed after update
  // which would in turn prevent the script from ending
  knex.destroy();

关于javascript - knex.js - 读取然后更新会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34342262/

相关文章:

node.js - 使用 yo Generator 创建meanjs 项目时出错

ios - 如何获取存储在 iOS 持久性 sqlite 数据库中的值?

android - 网络.sqlcipher.database.SQLiteException : not an error:

javascript - 从命令行执行 HTML Javascript

javascript - AJAX 呈现的表单不发送 RAILS 中的参数

node.js - 使用 Node.js http GET 请求发送浏览器、referer 和 cookie

android - 在手机(ios android)跨平台QT上调用sqlite数据库路径

javascript - 尝试获取样式时出现语法错误

javascript - 如何通过react router传递和使用查询参数?

mysql - PostgreSQL - 列不存在(WHERE with AS)