Rajesh
Rajesh

Reputation: 3014

DOM IDBDatabase Exception 11 when trying to create a transaction

I am using latest google chrome..I am trying to implement HTML5 indexedDB example. AIM is create database when page loads and store data in it. Please post any sample working example if any one have that. I tried this code but I am getting Uncaught Error: InvalidStateError: DOM IDBDatabase Exception 11 when I am trying to create a transaction. Here is the code snippet..

var idbRequest;
    var idb;
$(document).ready(function(){
    //ini----------------------
     var peopleData = [
                       { name: "John Dow", email: "[email protected]" },
                       { name: "Don Dow", email: "[email protected]" }
                   ];
    window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB;
    window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
    window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
    //create database.............
    if (window.indexedDB) {
          idbRequest = window.indexedDB.open("StoreDB");
          idbRequest.onsuccess =  function(e) {
              idb = idbRequest.result || e.result;  // FF4 requires e.result. IDBRequest.request isn't set :(
          /*idbRequest.onerror = function (evt) {
                  console.log("IndexedDB error: " + evt.target.errorCode);
              };*/
              var v = '2.0';
                var setVrequest = idb.setVersion(v);
                setVrequest.onsuccess = function(e) {
                    if(idb.objectStoreNames.contains("Stores")) {
                        alert("ObjectStore is already created.");
                        return false;
                      }
                    var objectstore = idb.createObjectStore("Stores");
                    alert("Object store Created.");
                    if (!idb.objectStoreNames.contains('Stores')) {
                          alert("Object store doesn't exist.");
                        return;
                      }else{
                          alert("Object store contain 'store'");
                      }

                       // Create a transaction that locks the world.
                     var trans = idb.transaction(["Stores"],"readwrite");//getting exception on this line..
                     trans.oncomplete = function(){
                          console.log("Success transaction");
                        };
                        var objectStore = trans.objectStore("Stores");
                      var request = objectStore.put(
                          1,
                          "wsdsdsd");
                    alert("data added");
                };






            };


    }   
});

Upvotes: 1

Views: 3205

Answers (2)

Kristof Degrave
Kristof Degrave

Reputation: 4180

You can find a working indexeddb sample at my site. I'm using a library that I wrote for the indexeddb API called linq2indexeddb. Try it out and it will save you a lot of effort making your code crossborwser compatible.

Upvotes: 0

Rajesh
Rajesh

Reputation: 3014

Solved.. I was getting error because we cannot create a transaction while version change is in progress..here is the final working answer..

var idbRequest;
    var idb;
$(document).ready(function(){
    //ini----------------------

    window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB;
    window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
    window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
    //create database.............
    if (window.indexedDB) {
          idbRequest = window.indexedDB.open("DB");
          idbRequest.onsuccess =  function(e) {
              idb = idbRequest.result || e.result;  // FF4 requires e.result. IDBRequest.request isn't set :(
          /*idbRequest.onerror = function (evt) {
                  console.log("IndexedDB error: " + evt.target.errorCode);
              };*/
              var v = '2.0';
                var setVrequest = idb.setVersion(v);
                setVrequest.onsuccess = function(e) {
                    if(idb.objectStoreNames.contains("Stores")) {
                        alert("ObjectStore is already created.");
                        var vertrans = setVrequest.result;
                        vertrans.oncomplete = doStuff;
                        return false;
                      }
                    var objectstore = idb.createObjectStore("Stores");
                    alert("Object store Created.");
                    if (!idb.objectStoreNames.contains('Stores')) {
                          alert("Object stores doesn't exist.");
                        return;
                      }else{
                          alert("Object store contain 'stores'");
                      } 
                    var vertrans = setVrequest.result;
                    vertrans.oncomplete = doStuff;
                };

            };


    }   
});

function doStuff(){
       // Create a transaction that locks the world.
    var trans = '';

         trans = idb.transaction(['Stores'],'readwrite');

     var objectStore = trans.objectStore('Stores');
      var request = objectStore.put(
          1,
          "wsdsdsd");
    alert("data added");
     trans.oncomplete = function(){
          console.log("Success transaction");
        };
}

Upvotes: 4

Related Questions