最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Counting the number of records in an object store in indexedDB - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited May 25, 2014 at 5:57 Josh 18.7k7 gold badges54 silver badges72 bronze badges asked Mar 18, 2014 at 15:48 BrentBrent 2,48510 gold badges41 silver badges64 bronze badges 1
  • Just to clarify, you'd like to count the number of objects in your objectstore rather than the database? You can count the number of objectstores at the database-level but not objects themselves. – buley Commented Mar 18, 2014 at 15:54
Add a comment  | 

3 Answers 3

Reset to default 14

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);
};

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.

发布评论

评论列表(0)

  1. 暂无评论