chetan mehta
chetan mehta

Reputation: 369

Properties/methods cant be initialised indexedDB

Couldn't understand why this is happening:

var request=window.indexedDB.open("known");    //async IDB request
request.onsuccess=function(){db=event.target.result;
                             alert("database created"+db);  //it works fine database created
                             var store=db.createObjectStore("friends",{pathKey:"name"})  
                             //error **"Uncaught InvalidStateError: An operation was called on an object on which it is not allowed or at a time when it is not allowed."** as on console box                                
                            }

When db has been assigned with reference to Database "known" then why error pops up?

Upvotes: 0

Views: 152

Answers (2)

dgrogan
dgrogan

Reputation: 2720

You can only call createObjectStore when you are in a versionchange transaction, which rougly corresponds to the upgradeneeded event handler. Also, it's "keyPath", not "pathKey". Try

var request=window.indexedDB.open("known", 2);    //async IDB request
request.onupgradeneeded = function() {
  console.log("got upgradeneeded event");
  db = event.target.result;
  var store = db.createObjectStore("friends", {keyPath: "name"});
}
request.onsuccess=function(){
  console.log("got success event");
  db=event.target.result;                                
}

There are some good examples in the spec.

Upvotes: 1

Josh
Josh

Reputation: 18690

Looks like you forgot to name the argument to the callback? Try:

request.onsuccess = function(event) ...

This way, "event" is defined.

Upvotes: 0

Related Questions