[英]IndexedDB DOM IDBDatabase Exception 11 even after using oncomplete
I am very new to IndexedDB Concepts. 我是IndexedDB Concepts的新手。 I am trying to Store a list of movies in the IndexedDB and retrieve it.
我试图在IndexedDB中存储电影列表并检索它。 But for some reason when i try to retrieve it there is a DOM IDBDatabase Exception 11 in chrome browser.
但由于某些原因,当我尝试检索它时,Chrome浏览器中有一个DOM IDBDatabase异常11。 I try to retrieve it by using a simple alert.
我尝试使用简单的警报来检索它。 I also tried to retrieve the data by putting the alert inside an onComplete event, but this too seems to be a failure.
我还尝试通过将警报放在onComplete事件中来检索数据,但这似乎也是失败的。 Could someone please let me know what wrong i am doing.
有人可以让我知道我在做什么错。 Below is my code.
以下是我的代码。
const dbName = "movies";
var request = indexedDB.open(dbName, 1);
request.onerror = function(event) {
alert("Seems like there is a kryptonite nearby.... Please Check back later");
};
request.onsuccess = function(event) {
var db = event.target.result;
var transaction = db.transaction(["movies"],"readwrite");
var objectStore = transaction.objectStore("movies");
var request1 = objectStore.get("1");
request1.result.oncomplete=function(){
alert("The movie is"+request1.result.name);//This is the place where i get the error
}
};
request.onupgradeneeded = function(event) {
db = event.target.result;
var objectStore = db.createObjectStore("movies", { keyPath: "movieid" });
objectStore.createIndex("name", "name", { unique: false });
objectStore.createIndex("runtime", "runtime", { unique: false });
for (var i in movieDataToStore) {
objectStore.add(movieDataToStore[i]);
}};
I still do not know what was wrong with the last program. 我仍然不知道最后一个程序出了什么问题。 i re-wrote the above program and it worked like a charm.
我重写了上面的程序,它就像一个魅力。 here is the code.
这是代码。 Hope this helps anyone who is stuck with this problem.
希望这可以帮助任何坚持这个问题的人。 Also if anyone figures out what went wrong the last time please share your thoughts.
如果有人在最后一次弄清楚出了什么问题,请分享您的想法。
var db; //database will be stored in this value when success is called
var movieDataToStore = [{ movieid: "1", name: "Keep my Storage Local", runtime:"60"},
{ movieid: "2", name: "Rich Internet Conversations", runtime:"45"},
{ movieid: "3", name: "Applications of the Rich and Famous", runtime:"30"},
{ movieid: "4", name: "All Jump All eXtreme", runtime:"45"}];
window.query = function() {
db.transaction("movies").objectStore("movies").get("1").onsuccess = function(event) {
alert("QUERY: CThe first movie is" + event.target.result.name);
};};
window.onload = function() {
if (!window.indexedDB) {
window.alert("Your browser doesn't support a stable version of IndexedDB. Such and such feature will not be available.")
}
else{
var request = indexedDB.open("movies", 1);
request.onerror = function(event) {
alert("Seems like there is a kryptonite nearby.... Please Check back later");
};
request.onsuccess = function(event) {
db = this.result;
query();
};
request.onupgradeneeded = function(event) {
var db = event.target.result;
if(db.objectStoreNames.contains("movies")) {
db.deleteObjectStore("movies");
}
var objectStore = db.createObjectStore("movies", { keyPath: "movieid"});
objectStore.createIndex("name", "name", { unique: false });
objectStore.createIndex("runtime", "runtime", { unique: false });
for (var i in movieDataToStore) {
objectStore.add(movieDataToStore[i]);
}
};
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.