amol01
amol01

Reputation: 1863

IndexedDB open DB request weird behavior

I have an app (questionnaire) that uses indexedDB. We have one database and several stores in it.

Stores have data already stored in them.

At some point a dashboard html file is loaded. In this file I am calling couple of functions:

function init(){
            adjustUsedScreenHeight();
            db_init();
            setInstitutionInstRow();
            loadRecommendations();
            loadResultsFromDB();
            fillEvaluations();
            document.addEventListener("deviceready", onDeviceReady, function(e) {console.log(e);}); 
        }

The init() function is called on body onLoad.

setInstitutionInstRow() looks like these:

 function setInstitutionInstRow(localId){
//localId = 10;
if (localId == undefined){
    console.log("Localid underfined: ");
    //open db, open objectstore;
    var request = indexedDB.open("kcapp_db", "1.0");

    request.onsuccess = function() {
        var db = request.result;    
        var tx = db.transaction ("LOCALINSTITUTIONS", "readonly");
        var store = tx.objectStore("LOCALINSTITUTIONS");

        tx.oncomplete = function(){
            db.close();
        }
        tx.onerror = function(){
            console.log("Transaction error on setInstInstRow");
        }

        var cursor = store.openCursor();
        cursor.onsuccess= function () {
            var match = cursor.result;
            console.log ("Retrieved item: " + match.value.instid);
            //  alert("Added new data");
            if (match){
                setInstituionInstRow(match.value.instid);
                console.log("Got localid: " + math.value.instid);
            }
            else
                console.log("localinsid: it is empty " );
        };

        cursor.onerror = function () {
            console.log("Error: " + item.result.errorCode);
        }

    }

    request.onerror = function () {
        console.log("Error: " + request.result.errorCode );
    }

    request.oncomplete = function (){
        console.log("The transaction is done: setInstitutionRow()");
    }

    request.onupgradeneeded = function (){
        console.log("Upgrade needed ...");
    }

    request.onblocked = function(){
        console.log("DB is Blocked ...");
    }


} else {
    instid = localId;
    var now = new Date();
    //console.log("["+now.getTime()+"]setInstituionInstRow - instid set to "+localId);
    //open db, open objectstore;
    var request = indexedDB.open("kcapp_db", "1.0");

    request.onsuccess = function() {
        var db = this.result;   
        var tx = db.transaction ("INSTITUTIONS", "readonly");
        var store = tx.objectStore("INSTITUTIONS");

        var item = store.get(localId);
        console.log(item);

        item.onsuccess= function () {
            console.log ("Retrieved item: ");
            if (item.length > 0)
                var lInstitution = item.result.value;
                kitaDisplayValue = lInstitution.krippe;     
            };

        item.onerror = function () {
            console.log("Error: " + item.result.errorCode);
        }

    }


    request.onerror = function () {
        console.log("Error: " + request.result.errorCode );
    }


}

Now the problem is,

var request = indexedDB.open("kcapp_db", "1.0");

the above request is never getting into any onsuccess, oncomplete, onerror states. I debugged with Chrome tools, it never getting into any above states.

Accordingly I am not getting any data from transactions.

And there are no errors in Chrome console.

And here is the request value from Chrome dev:

enter image description here

From above image the readyState: done , which means it should fire an event (success, error, blocked etc). But it is not going into any of them.

I am looking into it, and still can not figure out why it is not working.

Have to mention that the other functions from init() is behaving the same way.

Looking forward to get some help.

Upvotes: 0

Views: 811

Answers (3)

David F.
David F.

Reputation: 1

I had that problem but only with the "onupgradeneeded" event. I fixed it changing the name of the "open" function. At the begining I had a very long name; I changed it for a short one and start working. I don't know if this is the real problem but it was solved at that moment.

My code:

if (this.isSupported) {
            this.openRequest = indexedDB.open("OrdenesMant", 1);

            /**
             * Creación de la base de datos con tablas y claves primarias
             */ 
            this.openRequest.onupgradeneeded = function(oEvent) {
...

Hope it works for you as well.

Upvotes: 0

dgrogan
dgrogan

Reputation: 2720

  1. Like Josh said, your version parameter should be an integer, not a string.
  2. Your request object can get 4 events in response to the open request: success, error, upgradeneeded, or blocked. Add event listeners for all of those (e.g. request.onblocked = ...) and see which one is getting fired.

Upvotes: 1

Josh
Josh

Reputation: 18690

You may be using an invalid version parameter to the open function. Try indexedDB.open('kcapp_db', 1); instead.

Upvotes: 1

Related Questions