大网数据广告活动
Web 标准与前端开发

IDBRecord

· 52CE 我爱测

The **`IDBRecord`** interface of the IndexedDB API represents a snapshot of a single record in an IDBObjectStore or IDBIndex.

IndexedDB

The IDBRecord interface of the IndexedDB API represents a snapshot of a single record in an IDBObjectStore or IDBIndex.

A request for records using IDBObjectStore.getAllRecords() or IDBIndex.getAllRecords() returns an IDBRequest instance. On success, the returned object's IDBRequest.result property is populated with an array of IDBRecord instances.

Instance properties

  • key
    • : A value representing the record's secondary key. For an object store record, this will be the same as primaryKey. For an index record, it will be the record's key within the index.
  • primaryKey
    • : A value representing the record's primary key. This key is used to represent the record in the IDBObjectStore.
  • value
    • : A value representing the record's value.

Instance methods

None.

Examples

Basic usage

This example queries an IDBObjectStore for up to 100 records whose keys come after "myKey", with results sorted in reverse order.

The code first creates a transaction on an IDBDatabase named db (omitting the code to open the database), and then uses it to get an IDBObjectStore containing a contacts list. It then calls getAllRecords() on the object store, returning an IDBRequest instance. Event listeners are added to this request for the success and error events. On success, the result event.target.result is logged (this is also available as request.result). This result contains an array of IDBRecord instances. Note that because this is a query on an IDBObjectStore, the key and primaryKey in each record have the same value.

// Create a transaction on the database and use it to get the contained store
const transaction = db.transaction(["contactsList"], "readonly");
const objectStore = transaction.objectStore("contactsList");

const query = IDBKeyRange.lowerBound("myKey", true);

const request = objectStore.getAllRecords({
  query,
  count: 100,
  direction: "prev",
});

request.addEventListener("success", (event) => {
  const myRecords = event.target.result; // Array of IDBRecord instances
  console.log(myRecords);
});

request.addEventListener("error", (event) => {
  console.error("Error retrieving records:", event.target.error);
});

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。