最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - knex.js db call does not complete - Stack Overflow

programmeradmin0浏览0评论

I am learning node.js and came across knex.js and bookshelf.js to interface with different databases. I am trying to run a simple knex program but somehow the program doesnt quit. 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. Is my problem due to incomplete exit from a promise? Thanks

I am learning node.js and came across knex.js and bookshelf.js to interface with different databases. I am trying to run a simple knex program but somehow the program doesnt quit. 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. Is my problem due to incomplete exit from a promise? Thanks

Share Improve this question asked Mar 5, 2015 at 2:49 Shankhoneer ChakrovartyShankhoneer Chakrovarty 6851 gold badge6 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

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):

.finally(function() {
  knex.destroy();
})
发布评论

评论列表(0)

  1. 暂无评论