计算indexedDB中对象库中的记录数 [英] Counting the number of records in an object store in indexedDB

查看:64
本文介绍了计算indexedDB中对象库中的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to basic count the number of records in my indexedDB database.

目前我的代码看起来像

Javascript

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

我很乐意这样说输出只有3,但我得到了。

I would love for this to say output just 3, but instead i get.

输出

        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.

推荐答案

按顺序介绍一点点。来自我的个人文档交易:

A little bit of introduction in order. From my personal docs on transactions:


某些事务从数据库返回数据或结果。
这些事务称为请求,除了
数据库打开外,值始终是对象
keys和values以及IDBRequest实例的各种组合。请求事务
只是:一个事务请求,即要求
而不是获取它的行为。程序员在处理IDBObjectStore,IDBIndex或IDBCursor对象时会遇到

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.

你所看到的是一个 IDBRequest 对象,它是由 count()方法返回。这表示数据请求,而不是数据本身。

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.

完成事件触发后,数据本身可用,并且可以通过 IDBRequest.result 属性访问。

The data itself is available after the complete event fires, and can be accessed via the IDBRequest.result property.

这是来自我的图书馆,破折号

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

我会注意到我可能应该使用完成事件而不是成功事件。我无法解释原因,但有时结果值在 success 回调中不可用。

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.

这篇关于计算indexedDB中对象库中的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆