我需要检查某个表在打开后是否已经存在于 IndexedDB 中。但我不知道如何在 'then' 语句中获取 DexieDB 对象。
this.db = new Dexie("DBNAME");
if (!this.db.isOpen()) {
this.db.open().then(function () {
//how to get this.db.table(storeName) here?
}).catch(function (error) {
console.log(error)
});
}
所以 this.db
不存在于 'then' 语句中。如何获取?
最佳答案
在 map 上
特别是在 Dexie 中,你不必像那样调用 isOpen
和 open()
,你可以只调用 .open
等等将像这样工作:
// Declare db instance
var db = new Dexie("MyDatabase");
// Define Database Schema
//...
// Open Database
db.open();
db.trasnaction(...
一般情况
这是一个经典的 JS 上下文值。 this
在 JavaScript 中的工作方式不同 - here is the canonical reference about it which you should read .
此外 - 关于在 then 链中传递参数你应该引用 this excellent Q&A涵盖了更一般的方法
那里描述的解决方法(有上下文)通常适用并包含更多库特定代码,可以在这里为您提供帮助。
关于javascript - 如何在刚刚通过 Dexie.js 打开 indexedDB 后获取表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28293962/