我尝试创建的 indexedDB 出现错误。 “未捕获的 DOMException:无法在“IDBObjectStore”上执行“索引”:找不到指定的索引。”它说没有找到,但我相信我在几行之前正确创建了索引。
附注- 我正在学习,因为我对我来说太赤裸裸了,我对indexedDB不太了解
代码如下
//Credit @alexeagleson - github
// This works on all devices/browsers, and uses IndexedDBShim as a final fallback
const indexedDB =
window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB ||
window.shimIndexedDB;
//window.indexedDB.deleteDatabase("MeteorDynamicImportCache");//
//window.location.reload();//
// Open (or create) the database
const request = indexedDB.open("Workouts", 1);
request.onerror = function (event) {
console.error("An error occurred with IndexedDB");
console.error(event);
};
// Create the schema on create and version upgrade
request.onupgradeneeded = function () {
const db = request.result;
const store = db.createObjectStore("workouts", { keyPath: "id" });
store.createIndex("name_Type", ["Type"], { unique: false });
store.createIndex("specifics", ["Type","Where","For"], {unique: false,});
};
request.onsuccess = function () {
console.log("Database opened successfully");
const db = request.result;
const transaction = db.transaction("workouts", "readwrite");
const store = transaction.objectStore("workouts");
const nameIndex = store.index("name_Type"); [tag:ERROR HERE]
const specificsIndex = store.index("specifics");
我尝试改变字母大小写,因为这似乎解决了我最后遇到的类似错误。我真的不知道还能做什么。
我也遇到了同样的问题,我通过删除数据库解决了它。 您可以在开发工具中查看索引是否已创建。创建的索引应该在对象存储中可见。事实上,正如评论中所指出的,您可以使用 onupgradeneeded 事件更新数据库,但它可能不起作用,here可能就是原因。