Question

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.

Was it helpful?

Solution 3

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.

OTHER TIPS

Bring back record count with a little less code:

var store = db.transaction(['trans']).objectStore('trans');
var count = store.count();
count.onsuccess = function() {
    console.log(count.result);
}

Try something like this:

var transaction = db.transaction(["data"], "readonly");
var objectStore = transaction.objectStore("data"); 
var count = objectStore.count();

count.onsuccess = function() {
    console.log(count.result);
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top