无法在 Node js 中创建书架 orm Table
Cant create bookshelf orm Table in Node js
因此,我似乎无法使用 Node js 中的书架 orm 创建 tables,而且我不太清楚我的错误在哪里。这是创建 table 的代码:
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 上调用它“
”
未创建table,因为未调用knex.schema.createTable
方法。您只在 db_exportup
函数中定义了 up
方法,但没有调用它。
要根据对 '/services/adddb.php'
路由的请求创建 table,您需要调用 knex.schema.createTable 方法和 return 对成功执行承诺对象的响应return编辑:
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");
});
}
因此,我似乎无法使用 Node js 中的书架 orm 创建 tables,而且我不太清楚我的错误在哪里。这是创建 table 的代码:
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 上调用它“
”未创建table,因为未调用knex.schema.createTable
方法。您只在 db_exportup
函数中定义了 up
方法,但没有调用它。
要根据对 '/services/adddb.php'
路由的请求创建 table,您需要调用 knex.schema.createTable 方法和 return 对成功执行承诺对象的响应return编辑:
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");
});
}