简体   繁体   中英

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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