问题描述
我已经看到多个JavaScript示例使用 createIndex
在ObjectStore创建后直接定义ObjectStore索引:
I have seen multiple JavaScript examples of using createIndex
to define an ObjectStore index directly after the ObjectStore has been created like this:
var objectStore = ixDb.createObjectStore(osName, { keyPath: pkName, autoIncrement: autoIncrement });
objectStore.createIndex("name", "name", { unique: false });
任何人都可以告诉我如何使用 createIndex
在预先存在的表上没有调用 createObjectStore
?我想这里真正的问题是如何在不使用 createObjectStore
的情况下获取对objectStore的引用?
Can anyone show me how you would use createIndex
on a pre-existing table without calling createObjectStore
? I guess the real question here is how to get a reference to the objectStore without using createObjectStore
?
我尝试了几个没有运气的以下变化:
I tried several variations of the following with no luck:
var objectStore = window.IDBTransaction.objectStore(ObjectStoreName);
var index = objectStore.createIndex(ixName, fieldName, { unique: unique, multiEntry: multiEntry });
推荐答案
你在期间执行onupgradeneeded
,它应该与您首先创建对象存储的位置相同。在那里,您可以执行升级所需的任何操作,例如创建新索引。
You do it during onupgradeneeded
, which should be the same place you are making the object store in the first place. In there, you can do whatever appropriate action is needed to upgrade it, such as creating a new index. Here is an example.
这篇关于使用Javascript在IndexedDB中为预先存在的ObjectStore添加索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!