我见过多个使用 createIndex
在创建 ObjectStore 后直接定义 ObjectStore 索引的 JavaScript 示例,如下所示:
var objectStore = ixDb.createObjectStore(osName, { keyPath: pkName, autoIncrement: autoIncrement });
objectStore.createIndex("name", "name", { unique: false });
谁能告诉我如何在不调用 createObjectStore
的情况下在预先存在的表上使用 createIndex
?我想这里真正的问题是如何在不使用 createObjectStore
的情况下获取对 objectStore 的引用?
我尝试了以下几种变体,但没有成功:
var objectStore = window.IDBTransaction.objectStore(ObjectStoreName);
var index = objectStore.createIndex(ixName, fieldName, { unique: unique, multiEntry: multiEntry });
最佳答案
您在 onupgradeneeded
期间执行此操作,这应该与您首先创建对象存储的位置相同。在那里,您可以执行升级所需的任何适当操作,例如创建新索引。 Here is an example.
关于javascript - 使用 Javascript 向 IndexedDB 中预先存在的 ObjectStore 添加索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11532935/