javascript - Knex:为什么在没有下一次调用“then”的情况下调用“create table”不执行
问题描述
我面临下一个问题。如果我试图创建表,调用这些方法 - 没有结果:
this.knexDB.schema
.createTable("students", table => {
table.increments("id");
table.string("student_name");
table.string("studnt_number");
});
该表不创建。但是,如果我添加“then”的调用 - 表格会立即创建:
this.knexDB.schema
.createTable("students", table => {
table.increments("id");
table.string("student_name");
table.string("studnt_number");
})
.then(() => {
console.log("table created!");
});
问题:
- 为什么它会这样工作?
- 有什么方法可以
create table
使用更“合适”的方式立即调用?
解决方案
Knex
是一个查询构建器库,它具有可链接的 api,允许您链接不同的方法调用来构造查询。
事实上,db 查询实际上是一个异步操作,lib 作者决定在调用 api 之类的伪 promise 时“调用”db 查询。因此有必要调用then
。
如果你正在使用async
&await
你可以使用它,将隐式wait
调用。then
await this.knexDB.schema.createTable('students', table => {
table.increments('id');
table.string('student_name');
table.string('studnt_number');
});
推荐阅读
- sql - 使用什么 SQL 函数而不是 UNION 来只有一个条件?
- tensorflow - 在 Google Colab 上的 TPU 实例上运行时出现 InternalError: Failed to serialize message
- azure - 我可以使用需要映像计划的自定义映像创建 Azure Batch 池吗?
- azure-data-studio - Azure Data Studio - 未列出数据库
- java - “POM”类型的依赖
- javascript - 为什么每次都需要重启 npm 服务器?
- java - 如何在 Java 中读取 SQL Server 日志 (XEL) 文件
- javascript - PHP 解析从 Internet Explorer 发送的 Ajax 请求
- javascript - CSS & jQuery:元素移动元素
- gradle - 发布新工件时如何自动重建 Sonatype Nexus 的索引?