语言

Menu
Sites
Language
IndexedDB not retrieving data/re-opening

I've created a web application with an IndexedDB. After putting some data in it, I closed the app (long pressing the home button/pressing back in the main page). When going back to my app, the database is opened, but no data shows up. When checking the inspector I found out that this time the indexedDB tab was empty.

After first startup: http://i.imgur.com/z2NKmiH.png

After entering some data: http://i.imgur.com/XQKLieC.png

After quitting app & starting it back up: http://i.imgur.com/FotUk4d.png (NOTE: It stays like this, even when adding data)

Open IndexedDB code:

var dbName = "database";
var dbVersion = 1;

database.indexedDB.open = function() {
   var request = indexedDB.open(dbName, dbVersion);

   request.onsuccess = function(e) {
      console.log("database opened");
      var database = e.target.result;
      //Do stuff with database
   }
   
   request.onupgradeneeded = function(e) {
      var database = e.target.result;
		
      try {
         if (database.objectStoreNames &&
                             database.objectStoreNames.contains("entry"){
	    db.deleteObjectStore("entry");
         }
      }
      catch (err) {
         console.log("got err in objectStoreNames:" + err);
      }
      var store = db.createObjectStore("entry",{keyPath: "timestamp"});
      console.log("-- onupgradeneeded store:"+ JSON.stringify(store));
   }

   request.onfailure = function(e) {
      console.error("could not open database: "+e);  
   }

   request.onerror = function(e) {
      console.error("Err: "+e);
   }
};

In the logs I get "database opened", so everything should have gone alright, but I do not see any data. I can't find a solution anywhere, so I figured I'd try here. Does anyone know what I might be doing wrong? Thanks in advance!

编辑者为: Tiana Sweeney 03 11月, 2013

响应

5 回复
Imgen Tata

I don't the reason, but I have been using indexeddb with IDBWrapper (https://github.com/jensarps/IDBWrapper), with no issue at all. I don't use raw indexeddb api though, maybe you can give it a try

Tiana Sweeney

Thank you for your response and the link! I will implement this and see if this might solve my problems. 

Imgen Tata

You are welcome.

Tiana Sweeney

It worked like a charm! :) Thanks for the link!

Andrew Richeson

Thanks Imgen Tata share good knowledge