Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of records in an object store in indexedDB

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.

like image 530
Brent Avatar asked Mar 18 '14 15:03

Brent


2 Answers

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);
}
like image 110
AssemblyX Avatar answered Sep 30 '22 12:09

AssemblyX


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);
};
like image 28
Manikandan Krishnamoorthy Avatar answered Sep 30 '22 11:09

Manikandan Krishnamoorthy