IndexedDB: NotFoundError: DOM IDBDatabase Exception 8

2,496 views
Skip to first unread message

Giuseppe Lufrano

unread,
Sep 12, 2012, 12:32:15 PM9/12/12
to chromiu...@chromium.org
Hi, I'm just trying to use the Indexed Database Api, looking on web i found a simple code to test this api.
I've changed a lot of the original code to adapt it to my needs, and now i've a problem.
If i try to run the code on Chromium (v 23.0.1264.0 (156235)) it works really fine, but if i try the same code on Chrome (v 21.0.1180.89 m) it gives me a NotFoundError: DOM IDBDatabase Exception 8.
I'm using this script :

var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
var db;
(function () { 
    function initDb() {
        var request = indexedDB.open("UsersDB", 1);  
        request.onsuccess = function (evt) {
            db = request.result;                                                            
        };
 
        request.onerror = function (evt) {
            console.log("IndexedDB error: " + evt.target.errorCode);
        };
 
        request.onupgradeneeded = function (evt) {                   
            var objectStore = evt.currentTarget.result.createObjectStore("users", { keyPath: "id", autoIncrement: true });
            objectStore.createIndex("name", "name", { unique: false });
            objectStore.createIndex("email", "email", { unique: true });
        };
    }
 
    function contentLoaded() {
        initDb();                
        var btnAdd = document.getElementById("btnAdd");
        var btnDelete = document.getElementById("btnDelete");
        var btnPrint = document.getElementById("btnPrint");
        var btnClear = document.getElementById("btnClear");                  
 
        btnAdd.addEventListener("click", function () {
            var name = document.getElementById("txtName").value;
            var email = document.getElementById("txtEmail").value;
 
            var transaction = db.transaction(["users"], "readwrite");
            var objectStore = transaction.objectStore("users");                    
            var request = objectStore.add({ name: name, email: email });
            request.onsuccess = function (evt) {
                console.log("Record added!");
            };
        }, false);
                

       btnDelete.addEventListener("click", function () {
            var id = document.getElementById("txtID").value;
 
            var transaction = db.transaction(["users"], "readwrite");
            var objectStore = transaction.objectStore("users");
            var request = objectStore.openCursor();
            request.onsuccess = function(evt) {  
                var cursor = evt.target.result;  
                if (cursor) { 
                    if(cursor.key == id){
                        var delReq = objectStore.delete(cursor.key);

                        delReq.onsuccess = function(evt){
                            console.log("Deleted!");
                        }
                        delReq.onerror = function(evt){
                            console.log("Not Deleted!");
                        }
                    }          
                    cursor.continue();  
                }  
            };
            request.onerror = function(evt){
                console.log("Error!");
            };
        }, false);
 
        btnPrint.addEventListener("click", function () {
            var output = document.getElementById("printOutput");
            output.textContent = "";
 
            var transaction = db.transaction(["users"], "readonly");
            var objectStore = transaction.objectStore("users");
 
            var request = objectStore.openCursor();
            request.onsuccess = function(evt) {  
                var cursor = evt.target.result;  
                if (cursor) {  
                    output.textContent += "User: " + cursor.value.name + " email: " + cursor.value.email + " id: " + cursor.key + ".\n";                            
                    cursor.continue();  
                }else {  
                    console.log("Records");  
                }  
            };  
        }, false);

        btnClear.addEventListener("click", function () {
            var transaction = db.transaction(["users"], "readwrite");
            var objectStore = transaction.objectStore("users");                    
            var request = objectStore.clear();
            request.onsuccess = function (evt) {
                console.log("All Records deleted!"); 
            };
        }, false);              
    }
 
    window.addEventListener("DOMContentLoaded", contentLoaded, false); 
})();//function() 

Giuseppe Lufrano

unread,
Sep 12, 2012, 12:36:24 PM9/12/12
to chromiu...@chromium.org
I forgot. I get the error every time there is a   var transaction = db.transaction(["users"], "readwrite"); call.

David Grogan

unread,
Sep 12, 2012, 2:34:25 PM9/12/12
to Giuseppe Lufrano, chromiu...@chromium.org


On Wed, Sep 12, 2012 at 9:32 AM, Giuseppe Lufrano <gius.l...@gmail.com> wrote:
Hi, I'm just trying to use the Indexed Database Api, looking on web i found a simple code to test this api.
I've changed a lot of the original code to adapt it to my needs, and now i've a problem.
If i try to run the code on Chromium (v 23.0.1264.0 (156235)) it works really fine, but if i try the same code on Chrome (v 21.0.1180.89 m) it gives me a NotFoundError: DOM IDBDatabase Exception 8.
 
From

NotFoundError = One of the names provided in the storeNames argument doesn't exist in this database.

Chrome didn't fire the upgradeneeded event until chrome 23 so the "users" objectstore isn't created.


--
You received this message because you are subscribed to the Google Groups "Chromium HTML5" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msg/chromium-html5/-/DmbyoV0AO3MJ.
To post to this group, send email to chromiu...@chromium.org.
To unsubscribe from this group, send email to chromium-html...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-html5/?hl=en.

Giuseppe Lufrano

unread,
Sep 12, 2012, 3:46:08 PM9/12/12
to chromiu...@chromium.org, Giuseppe Lufrano
Good, this is why it works in chromium and not in chrome.
How can i solve it? Moving the code
 
            var objectStore = evt.currentTarget.result.createObjectStore("users", { keyPath: "id", autoIncrement: true });
            objectStore.createIndex("name", "name", { unique: false });
            objectStore.createIndex("email", "email", { unique: true }); 

from onupgradeneeded to onsuccess?

Joshua Bell

unread,
Sep 13, 2012, 3:33:26 PM9/13/12
to Giuseppe Lufrano, chromiu...@chromium.org
On Wed, Sep 12, 2012 at 12:46 PM, Giuseppe Lufrano <gius.l...@gmail.com> wrote:
Good, this is why it works in chromium and not in chrome.
How can i solve it? Moving the code
 
            var objectStore = evt.currentTarget.result.createObjectStore("users", { keyPath: "id", autoIncrement: true });
            objectStore.createIndex("name", "name", { unique: false });
            objectStore.createIndex("email", "email", { unique: true }); 

from onupgradeneeded to onsuccess?

No - creating object stores and indexes can only be done in a "versionchange" transaction.  When the "upgradeneeded" event is fired, a "versionchange" transaction has automatically been created for you.

With Chrome prior to 23 you need to create such a transaction manually by calling setVersion() - an API that has been removed from the spec. The older spec can be found at: http://www.w3.org/TR/2011/WD-IndexedDB-20110419/

IMHO, the easiest thing to do is to use a shim/polyfill to support the open(name, version) / "upgradeneeded" event versioning approach in older versions of Chrome, and drop the shim once 23 has gone stable. I posted such a shim at: https://gist.github.com/3361666 - but it is not rigorously tested.

  

David Grogan

unread,
Sep 12, 2012, 5:24:33 PM9/12/12
to Giuseppe Lufrano, chromiu...@chromium.org
Before version 23 we only used the obsolete setVersion mechanism.  There's an informative blog post[1] that links to a shim you can use for older chrome versions[2].  There's a different shim you could use[3] that doesn't require you to call openReqShim instead of indexedDB.open.  I haven't used either one so I can't vouch for them but others have had success.
On Wed, Sep 12, 2012 at 12:46 PM, Giuseppe Lufrano <gius.l...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages