axemclion / jquery-indexeddb

An IndexedDB Plugin for Jquery.
Other
195 stars 71 forks source link

Chrome 27 stable: NotFoundError: DOM IDBDatabase Exception 8 #37

Closed Ciantic closed 11 years ago

Ciantic commented 11 years ago

I'm trying to figure out how to use this beast (see jsFiddle), my first try:

$.indexedDB("databaseName",{version:3}).objectStore("objectStoreName").add("test").then().fail(function (e) { console.log(e); });

Throws me in console: NotFoundError: DOM IDBDatabase Exception 8 ...

(I also get this error in the tinkering tool)

Update: Btw same fiddle in Firefox throws me exception: indexedDB is undefined, what is going on? Is this thing broken version. In firebug I can clearly however see that window.indexedDB is defined!

Ciantic commented 11 years ago

Plot thickens: The unit tests works fine (except the global transaction tests but I suppose that is known) but if I remove the test Open Database with schema most of the tests doesn't work anymore. Even though I have Open Database without schema still defined.

I haven figured out yet how to use this thing.

P.S. Your unit tests are flawed if they depend on each others existence. Also one can't run individual tests from the button, which makes things also difficult to debug.

axemclion commented 11 years ago

@Ciantic - The way I have written the tests- they are dependent of each other. The schema test creates the required object stores and when that is removed, all tests fail as there is no schema.

axemclion commented 11 years ago

@Ciantic - The problem with your code above is two fold - 1, you choose a version, but do not specify an upgrade path. Unless you would like to have a migration path (create new object stores or indexes), you may not want to use a version. Additionally, the object store does not exist yet - pass a second try argument to the objectStore method to create it if it does not exist. Hence, the following code could work.

// Code to create object stores and add data
(function(){
$.indexedDB("databaseName", {
"schema": {
"1": function(versionTransaction){
versionTransaction.createObjectStore("objectStore1");
},
"2": function(versionTransaction){
versionTransaction.createObjectStore("objectStore2");
}
}
}).transaction(["objectStore1", "objectStore2"]).then(function(){
log("Transaction completed");
}, function(){
log("Transaction aborted");
}, function(t){
log("Transaction in progress");
t.objectStore("objectStore1").add({
"valueProp": "val",
"anotherProp": 2
}, 1).then(function(){
log("Data added");
}, function(){
log("Error adding data");
});
});
})
Ciantic commented 11 years ago

@axemclion can you create the simplest way to use this thing? Without versions? This doesn't work either:

$.indexedDB("databaseName").objectStore("objectStoreName").add("test").then().fail(function (e) { console.log(e); });

axemclion commented 11 years ago

@Ciantic - This will

$.indexedDB("databaseName").objectStore("objectStoreName", true).add("test").then().fail(function (e) { console.log(e); });

Note the second true in the objectStore, to create an objectStore if it does not exist.

Ciantic commented 11 years ago

Thank you very much!

You should add this gem to front page, people want to replace their localStorages (which suffers from being synchronous) with something simple and easiest examples are best way to get started.

axemclion commented 11 years ago

@Ciantic - Thank you for the suggestion - can you send me a pull request with this change ? I think you should get credit for this suggestion - and this snippet is a great way to get started with jquery-indexeddb.

Ciantic commented 11 years ago

I could do that, but I'd like to find out the simplest way to use this as asynchronous key value store, since that's what I think this should be, I tried this:

var myObjectStore = $.indexedDB("myDatabase").objectStore("customStoreName", true);

// Add to object store (notice that key is the second argument)
myObjectStore.add({'name' : 'Jack Smith'}, 'jack');

// Get from the object store
myObjectStore.get("jack").done(function(person) { 
    console.log("Gotcha!", person); 
}).fail(function () { 
    console.log("NOT FOUND!");
});

I'm still confused how to use this API. On first run this throws me "NOT FOUND" on second run it does give me Gotcha! but it also throws me Uncaught TypeError: Cannot call method 'close' of undefined somewhere inside the jquery.indexeddb.js.

axemclion commented 11 years ago

@Ciantic Can you create a JS Fiddle for this ?