Jeanluca Scaljeri
Jeanluca Scaljeri

Reputation: 29097

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:"[email protected]"},      
        {ssn:"555-55-5555",name:"Donna",age:32,email:"[email protected]"}
      ];

      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

Upvotes: 0

Views: 5519

Answers (2)

Jason Diamond
Jason Diamond

Reputation: 780

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.

Upvotes: 5

jsbuster
jsbuster

Reputation: 173

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

Upvotes: 1

Related Questions