无法在Node js中创建bookhelf orm表

时间:2016-03-23 00:26:52

标签: javascript node.js bookshelf.js

所以,我似乎无法使用Node js中的书架orm创建表格,我无法弄清楚我的错误在哪里。以下是创建表的代码:

    function db_exportup(response){
    exports.up = function(knex, Promise) {
            return knex.schema.createTable('users', function(table) {
            table.increments('id').primary();
            table.string('name');
        });
    };
    exports.down = function(knex, Promise) {
  return knex.schema.dropTable('users');
    };
    response.writeHead(200, {"Content-type" : "text/plain"});
  response.write("Table created!");
  response.end();
}

当有人请求“/services/adddb.php”时我在server.js上调用它

1 个答案:

答案 0 :(得分:0)

未创建表,因为未调用knex.schema.createTable方法。您只在up函数中定义了db_exportup方法,但没有调用它。

要根据'/services/adddb.php'路由的请求创建表,您需要调用knex.schema.createTable方法并在成功执行返回的promise对象时返回响应:

function db_exportup(request, response) {
  // Call the 'createTable' method
  knex.schema.createTable('users', function(table) {
    table.increments('id').primary();
    table.string('name');
  })
  .then(function() {
    // Table creation succeeded
    response.writeHead(200, {"Content-type" : "text/plain"});
    response.write("Table created!");
    response.end();
  })
  .catch(function() {
    // Table creation did not succeed.
    response.write("Table was not created");
  });
}