IDBRequest

IndexedDB api中的IDBRequest接口提供了根据绑定事件处理函数访问结果集的方法。其中结果集来自对数据库和数据库对象发起的异步查询。对数据库的读写操作都是通过request的方式来实现。
 

该request对象初始时不包括任何关于操作结果的信息,当request上的事件触发时,可以通过IDBRequest实例上的事件处理函数访问相关信息。

继承自: EventTarget

About this document

This document was last updated on August 17, 2012 and follows the W3C Specifications (Editor's Draft) drafted on July 24, 2012. It has not yet been verified.

基础概念

所有异步操作立即返回一个IDBRequest实例。每一个请求都有一个readyState属性,初始时为pending,当请求完成或失败的时候,readyState会变为done。当状态值变为done时,每一个请求都会返回result和error属性,并且会触发一个事件。当状态保持为pending时,任何尝试访问result或error属性的行为会触发一个InvalidStateError异常。

用直白的话来说就是:所有的异步方法返回一个request对象。如果request对象成功执行了,结果可以通过result属性访问到,并且该request对象上会触发success事件。如果操作中有错误发生,一个error事件会触发,并且会通过result属性抛出一个异常。

示例

下面的代码片段中,我们异步打开一个数据库并且发起一个请求。注册了几个事件处理函数来展示不同的情况。

var request = window.indexedDB.open('数据库名称');
request.onsuccess = function(event) {
        var db = this.result;
        var transaction = db.transaction([]);
// "readonly" is the default option;
// when data will be added to the database use "readwrite".
        var curRequest = transaction.objectStore('ObjectStore Name').openCursor();
        curRequest.onsuccess = ...;
    };
request.onerror = function(event) {
         ...;
    };
request.onupgradeneeded= function(event) {
         // changing objectStore data is done here, as opposed to a transaction enum:
         ...;
    };

Attributes

Attribute Type Description
result readonly any

Returns the result of the request.

If the the request failed and the result is not available, the InvalidStateError exception is thrown.

error readonly DOMError

The following error codes are returned under certain conditions:

  • AbortError — If you abort the transaction, then all requests still in progress receive this error.
  • ConstraintError — If you insert data that doesn't conform to a constraint. It's an exception type for creating stores and indexes. You get this error, for example, if you try to add a new key that already exists in the record.
  • QuotaExceededError — If you run out of disk quota and the user declined to grant you more space.
  • UnknownError — If the operation failed for reasons unrelated to the database itself. A failure due to disk IO errors is such an example.
  • NoError — If the request succeeds.
  • VersionError — If you try to open a database with a version lower than the one it already has.

In addition to the error codes sent to the IDBRequest object, asynchronous operations can also raise exceptions. The list describes problems that could occur when the request is being executed, but you might also encounter other problems when the request is being made. For example, if the the request failed and the result is not available, the InvalidStateError exception is thrown.

source readonly Object

The source of the request, such as an Index or a ObjectStore. If no source exists (such as when calling indexedDB.open()), it returns null.

transaction readonly IDBTransaction The transaction for the request. This property can be null for certain requests, such as for request returned from IDBFactory.open (You're just connecting to a database, so there is no transaction to return).
readyState readonly enum

The state of the request. Every request starts in the pending state. The state changes to done when the request completes successfully or when an error occurs.

onerror Function The event handler for the error event.
onsuccess Function The event handler for the success event.

Constants

readyState constants

已废弃 Gecko 25 (Firefox 25 / Thunderbird 25 / SeaMonkey 2.22)
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

These constants are no longer available. You should use directly the string constants instead. (bug 887524)

Constant Value Description
DONE "done" The request has completed or an error has occurred. Initially false
LOADING "pending" The request has been started, but its result is not yet available.

Event handlers

Event handler Event handler type
onerror error
onsuccess success

Derived interface

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 12 -webkit 4.0 (2.0) 未实现 未实现 未实现
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 未实现 6.0 (6.0) ? 未实现 未实现

 

文档标签和贡献者