Reputation: 2212
I have been following the page Using IndexedDB.
When the page is loaded, I create/load the database.
window.App = window.App || {};
App.db = window.indexedDB.open("myapp", 1);
Following these, I define onerror
, onupgradeneeded
and onsuccess
for App.db
object.
In onupgradeneeded
, I create a new object store.
Finally, the onsuccess
method is called automatically by Javascript because the database is created/loaded. In this method, I list the details of App.db
.
App.db.onsuccess = function(ev){
console.dir( App.db );
};
Console shows that,
readyState = "done"
result = IDBDatabase
transaction = null
I called the page with file:///, http://localhost, http://myownserver.that.is.forwarded.to.127.0.0.1.from.domain.provider separately.
but the problem persists. The transaction
method is null, and I can't do anything, start any transaction. What is the problem, what am I missing?
Upvotes: 0
Views: 317
Reputation: 8337
indexedDB.open
returns a request object (IDBRequest
). So your App.db
will hold the request.
It sounds like you have a handle on the upgrade process.
Once that's complete, the connection object (an IDBDatabase
instance) is returned via the result
property of the request, and you use the transaction()
method on the connection object to start transactions. So you'd want to write your "success"
handler more like:
App.db.onsuccess = function(ev){
var connection = App.db.result;
var tx = connection.transaction(stores, mode);
...
};
But it's more common to call the connection object something like db
and you don't need to hold on to the request once the connection is opened, so what you probably meant to do is something more like this:
var openRequest = indexedDB.open("myapp", 1);
openRequest.onupgradeneeded = function(e) {
var db = openRequest.result;
db.createObjectStore(...);
...
};
openRequest.onsuccess = function(e) {
App.db = openRequest.result;
var tx = App.db.transaction(...);
};
Upvotes: 2