I want to basic count the number of records in my indexedDB database.
Currently my code looks like
Javascript
var transaction = db.transaction(["data"], "readonly");
var objectStore = transaction.objectStore("data");
var cursor = objectStore.openCursor();
var count = objectStore.count();
console.log(count);
I would love for this to say output just 3, but instead i get.
Output
IDBRequest {onerror: null, onsuccess: null, readyState: "pending", transaction: IDBTransaction, source: IDBObjectStore…}
error: null
onerror: null
onsuccess: null
readyState: "done"
result: 3
source: IDBObjectStore
transaction: IDBTransaction
__proto__: IDBRequest
Which is correct but I just want it to say 3 not loads of other stuff.
A little bit of introduction in order. From my personal docs on transactions:
What you're looking at is an
IDBRequest
object, which is returned by thecount()
method. That represents the request for data, and not the data itself.The data itself is available after the
complete
event fires, and can be accessed via theIDBRequest.result
property.Here's a tested count method from my library, dash:
I'll note that I probably should have used the
complete
event rather than thesuccess
event. I can't explain why but sometimes result values are not available insuccess
callbacks.