indexeddbsencha-touch-2.3

indexedDB: issue calling transaction function


I am new to IndexedDB and sencha touch framework. While learning IndexedDB in sencha, I came across a problem, the solution which I searched a lot but could not find any.

Let me show you my chunk of code first

        var db;
        var myRequest = indexedDB.open("testDB", 8);

        myRequest.onupgradeneeded = function (e) {
            console.log("upgrading...");
            var thisDB= e.target.result;
            if(!thisDB.objectStoreNames.contains("FirstOS")){
            thisDB.createObjectStore("FirstOS");
            }
        };

        myRequest.onsuccess = function (e) {
            console.log("success...");
            db= e.target.result;
        };

        myRequest.onerror = function (e) {
            console.log("error occured");
        };


    var transaction = db.transaction(["FirstOS"],"readwrite");
    var store= transaction.objectStore("FirstOS");

It throws an exception saying

Uncaught TypeError: Cannot read property 'transaction' of undefined 

Any help would be appreciated. Thank you.


Solution

  • This error message is not specific to using indexedDB. The call to indexedDB.db.transaction() is what generates this error message. This error happens when you try and call a method of an object that is not initialized. To avoid this error, do not use var db outside of the scope of the onsuccess callback function. Do not try to use db=event.target.result;.

    This error occurs because you are probably not familiar with asynchronous functions. You will need to learn more about how to write asynchronous code before continuing to use indexedDB.

    This question is a duplicate of several similar questions: