[英]knex.js - reading then updating throws an Error
我正在嘗試鏈接讀取和更新操作。 首先,我根據id從數據庫讀取(獲取)數據,然后對其進行更改,並希望也更新數據庫中的數據,但是我收到Unhandled rejection Error: There is no pool defined on the current client
錯誤Unhandled rejection Error: There is no pool defined on the current client
。
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.