最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my onupgradeneeded callback never called when connecting to indexedDB? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited May 25, 2014 at 2:41 Josh 18.8k7 gold badges54 silver badges72 bronze badges asked Feb 1, 2012 at 8:22 Jeanluca ScaljeriJeanluca Scaljeri 29.2k66 gold badges235 silver badges382 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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

发布评论

评论列表(0)

  1. 暂无评论