javascript - 计算 indexedDB 中对象存储中的记录数

标签 javascript indexeddb

我想基本计算 indexedDB 数据库中的记录数。

目前我的代码看起来像

Javascript

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

我很想说输出只有 3,但我得到了。

输出

        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

这是正确的,但我只想说 3 而不是其他内容。

最佳答案

用更少的代码恢复记录数:

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

关于javascript - 计算 indexedDB 中对象存储中的记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22484336/

相关文章:

javascript - JavaScript 中的增强类型

javascript - 查询日期时间选择器无法在另一个函数调用中获取日期

javascript - 三个js的loader使用webworker和IndexedDB

javascript - 在 IndexedDB 中搜索复合索引

database - 我如何看待 "IndexedDB"?

Javascript 阻止调用

像 actionscript 的 normalize(1) 一样工作的 Javascript 函数

javascript - D3 从 csv 填充数据集失败

javascript - 我应该只使用 IndexedDB 还是同时使用 IndexedDB 和 chrome.storage?

javascript - 如何使用 JayData 为 indexedDB 中的表创建索引