A little bit of introduction in order. From my personal docs on transactions:
Certain transactions return data, or "results", from the database. These transactions are called "requests" and with the exception of database opening, the values are always various combinations of object "keys" and "values" and instances of IDBRequest. Request transactions are just that: a transaction request," namely the act of asking for something rather than the getting of it. A programmer encounters them when dealing with IDBObjectStore, IDBIndex or IDBCursor objects.
What you're looking at is an IDBRequest
object, which is returned by the count()
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 the IDBRequest.result
property.
Here's a tested count method from my library, dash:
API.entries.count = function (count_ctx) {
var request;
if (API.exists(count_ctx.index)) {
count_ctx.idx = count_ctx.objectstore.index(count_ctx.index);
request = API.isEmpty(count_ctx.key) ? count_ctx.idx.count() : count_ctx.idx.count(count_ctx.key);
} else {
request = API.isEmpty(count_ctx.key) ? count_ctx.objectstore.count() : count_ctx.objectstore.count(count_ctx.key);
}
count_ctx.transaction.addEventListener('error', function (event) {
count_ctx.error = event.target.error.message;
API.error(count_ctx);
});
request.addEventListener('success', function () {
count_ctx.total = request.result;
API.success(count_ctx);
});
I'll note that I probably should have used the complete
event rather than the success
event. I can't explain why but sometimes result values are not available in success
callbacks.