简体   繁体   English

IndexedDB DOM IDBDatabase例外11即使在使用oncomplete之后也是如此

[英]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]);
              }
    };

        }   
};
  1. I think it is bad practice to insert data in the onupgradeneeded context. 我认为在onupgradeneeded上下文中插入数据是不好的做法。 You should be doing this separately in an unrelated function at some other time. 您应该在其他时间以不相关的功能单独执行此操作。 In fact, attempting to insert the data on a database whose version was incremented since last page load will automatically trigger the upgradeneeded event for you. 实际上,尝试将数据插入自上次页面加载后版本增加的数据库将自动为您触发upgradeneeded事件。
  2. While many of the online examples shove the database connection handle (your db var) into some global scope variable, this is also a bad practice that will lead to errors down the road. 虽然许多在线示例将数据库连接句柄(您的db var)推送到某个全局范围变量中,但这也是一种不良做法,会导致错误。 Only access the db var within your callbacks as a parameter. 仅访问回调中的db var作为参数。 In other words, your openRequest.onsuccess function should pass the db variable to the query function. 换句话说,openRequest.onsuccess函数应该将db变量传递给查询函数。 This also reduces the chances of any garbage collection issues later and leaving database connections open (which the designers of indexedDB allow for, but should generally be avoided). 这也减少了以后任何垃圾收集问题的机会,并使数据库连接保持打开状态(indexedDB的设计者允许这样做,但通常应该避免)。
  3. If your movie ids are integers, it isn't clear to me why you are storing and retrieving them as strings. 如果你的电影ID是整数,我不清楚你为什么要存储它们并将它们作为字符串检索。 You can store integer values. 您可以存储整数值。 You can pass an integer to store.get. 您可以将整数传递给store.get。
  4. You are using for...in inappropriately. 你正在使用......不恰当。 It will work, but for...in is intended for looping over the keys of object literals (like var x = {key:value}). 它将起作用,但是对于... in用于循环对象文字的键(如var x = {key:value})。 Use a normal for loop or use array.foreach when iterating over your movies array. 使用普通for循环或在迭代影片阵列时使用array.foreach。
  5. As you found out in your fixed code, it is better to use request.onsuccess and request.onerror. 正如您在固定代码中发现的那样,最好使用request.onsuccess和request.onerror。 There is also a transaction.oncomplete. 还有一个transaction.oncomplete。 But I am not sure there is a request.oncomplete. 但我不确定是否有request.oncomplete。 What happens is you are setting the oncomplete property of an IDBRequestObject but this does nothing since the code never triggers it. 你正在设置IDBRequestObject的oncomplete属性会发生什么,但是由于代码永远不会触发它,所以没有做任何事情。
  6. DOM 11 usually signals you tried to access a table that does not exist or is in an incorrect state. DOM 11通常表示您试图访问不存在或处于不正确状态的表。 Usually this happens due to mistakes elsewhere, like onupgradeneeded never getting called when connecting. 通常这是由于其他地方的错误而发生的,例如onupgradeneeded在连接时永远不会被调用。 It is confusing, but given the way your code is setup, basically the db gets created the first time your page loads, but then never gets created again, so while developing, if you made changes, but do not increment your db version, you will never see them. 这很令人困惑,但考虑到代码的设置方式,基本上db会在你的页面第一次加载时创建,但是永远不会再次创建,所以在开发时,如果你做了更改,但是没有增加db版本,那么你永远不会见到他们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM