[英]knex.js db call does not complete
I am learning node.js and came across knex.js and bookshelf.js to interface with different databases. 我正在学习node.js并遇到了knex.js和bookshelf.js来与不同的数据库进行交互。 I am trying to run a simple knex program but somehow the program doesnt quit. 我试图运行一个简单的knex程序,但不知何故该程序不会退出。 Below is the program: 以下是该计划:
'use strict';
console.log('Getting knex');
var knex = require('./knex')({
client: 'mysql',
connection: {
host: '127.0.0.1',
user: 'shankhoneer',
password: 'password',
database: 'knex_test'
}
});
debugger;
console.log('got knex');
knex.schema.createTable('users', function(table) {
console.log('creating tables');
table.increments('id');
table.string('user_name');
}).then (function(msg){
console.log('Completed creation');
console.log(msg);
return {inserted: true};
});
I tried to debug and found that knex uses bluebird promises. 我试图调试,发现knex使用蓝鸟承诺。 Is my problem due to incomplete exit from a promise? 我的问题是由于承诺退出不完整吗? Thanks 谢谢
This is because the connection to the database is still open. 这是因为与数据库的连接仍然是打开的。 Add the following code after your last .then( ... )
and it will disconnect (thus exiting the process): 在最后一个.then( ... )
之后添加以下代码,它将断开连接(从而退出进程):
.finally(function() {
knex.destroy();
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.