Why is my onupgradeneeded callback never called when connecting to indexedDB?

浪子不回头ぞ 提交于 2019-12-11 15:37:50

问题


I'm trying to get some data stored locally using IndexedDB. Below I'm I have a simple example with which I'm trying to get the onupgradeneeded event to fire

<html>
  <head>
    <script>

      var indexedDB = window.indexedDB || window.webkitIndexedDB 
                    ||window.mozIndexedDB||window.msIndexedDB;

      var request = indexedDB.open("mydb",2),    

      customerData=[
        {ssn:"444-44-4444",name:"Bill",age:35,email:"bill@company.com"},      
        {ssn:"555-55-5555",name:"Donna",age:32,email:"donna@home.org"}
      ];

      request.onerror = function(event){
         alert("ERROR") ;
      };
      request.onupgradeneeded = function(event) {
         alert("UPGRADE NEEDED") ;
         var objectStore = db.createObjectStore("customers",{keyPath:"ssn"});
         objectStore.createIndex("name","name",{unique:false});
         objectStore.createIndex("email","email",{unique:true});

         for(var i in customerData){
            objectStore.add(customerData[i]);
         }
      } ;
      request.onsuccess = function(e) {
         alert("SUCCESS") ;
      } ;
    </script>
  </head>
</html>

I tried to change the version number but whatever I try onupgradeneeded is never called/fired. Any suggestions why ?

cheers Luca


回答1:


I tested this in FF 10 and it worked for me. (Until the code tried to use the undefined "db" variable".)

What browser are you using? The upgradeneeded event is only raised in FF 10. Chrome 16 still uses the old draft where you have to check the db.version property and call db.setVersion if it's not what you want.

If you are using FF10, are you sure that "mydb" isn't already created?

Also, be sure that you're testing this through a real web server. FF won't let you use indexedDB with local HTML files. If that's happening, you should see an error like "The operation failed for reasons unrelated to the database itself and not covered by any other error code" in your console.




回答2:


You didn't define the db variable

I have the same problem... this is the code from MDN, and they didn't explain how to update the DB version without the setVersion() function



来源:https://stackoverflow.com/questions/9092762/why-is-my-onupgradeneeded-callback-never-called-when-connecting-to-indexeddb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!