我有一个indexeddb,当我第一次在chrome中打开我的网站时,尝试调用init.savedb();我收到一个错误告诉 未捕获的DOMException:无法执行' put' on' IDBObjectStore':评估对象存储库的关键路径没有产生值。
然后我重新加载页面,一切正常。
有人知道我做错了什么吗?
这是代码:
var model = {
everything: {},
db: {},
goals: [],
tags: [],
init: function () {
var openReq = window.indexedDB.open("micromanagerv999");
openReq.onupgradeneeded = function (event) {
model.db = event.target.result;
var objectStore = model.db.createObjectStore("Everything", { keyPath: "id" });
objectStore.createIndex("id", "id", { unique: true });
};
openReq.onsuccess = function (event) {
model.db = event.target.result;
model.db.transaction("Everything", "readonly").objectStore("Everything").count().onsuccess = function (event) {
if (event.target.result === 0) {
var basictemplate = {
id: "idee",
goals: [],
};
var addReq = model.db.transaction("Everything", "readwrite").objectStore("Everything").add(basictemplate);
} else {
model.db.transaction("Everything", "readonly").objectStore("Everything").get("idee").onsuccess = function (e) {
model.everything = e.target.result;
model.goals = model.everything.goals;
if (model.everything.tags == undefined) {
model.everything.tags = [];
model.tags = model.everything.tags;
} else {
model.tags = model.everything.tags;
}
microGoals.renderArray();
};
}
};
openReq.onerror = function (event) {
console.log("Operation failed");
};
}
},
savedb: function () {
var update = model.db.transaction("Everything", "readwrite").objectStore("Everything").put(model.everything);
update.onerror = function (event) {
console.log(event);
}
}
}; // End of MODEL