IDBObjectStore.indexNames

IDBObjectStore 的只读属性 indexNames 返回此对象存储中对象的 indexes 名称(name)列表。

备注: 此特性在 Web Worker 中可用。

Syntax

var myindexNames = objectStore.indexNames;

Value

Example

在下面的代码片段中,我们在数据库上打开一个读/写事务并使用 add() 向对象存储添加一些数据。创建对象存储后,我们将打印 objectStore.indexNames 到控制台。有关完整的工作示例,请参阅我们的 待办事项通知应用程序 ( 实时查看示例 )

js
// 让我们来打开我们的数据库
var DBOpenRequest = window.indexedDB.open("toDoList", 4);

DBOpenRequest.onsuccess = function (event) {
  note.innerHTML += "<li>Database initialised.</li>";

  // 将打开数据库的结果存储在 db 变量中
  // 下面经常用到这个
  db = this.result;

  // 运行 addData() 函数将数据添加到数据库
  addData();
};

function addData() {
  // 创建一个新对象以准备插入到 IDB 中
  var newItem = [
    {
      taskTitle: "Walk dog",
      hours: 19,
      minutes: 30,
      day: 24,
      month: "December",
      year: 2013,
      notified: "no",
    },
  ];

  // 打开读/写数据库事务,准备添加数据
  var transaction = db.transaction(["toDoList"], "readwrite");

  // 当所有事情都完成时,报告事务完成的成功情况
  transaction.oncomplete = function (event) {
    note.innerHTML += "<li>Transaction completed.</li>";
  };

  transaction.onerror = function (event) {
    note.innerHTML +=
      "<li>Transaction not opened due to error. Duplicate items not allowed.</li>";
  };

  // 在事务上创建对象存储
  var objectStore = transaction.objectStore("toDoList");
  console.log(objectStore.indexNames);

  // 请求将 newItem 对象 添加到对象存储区
  var objectStoreRequest = objectStore.add(newItem[0]);

  objectStoreRequest.onsuccess = function (event) {
    // 报告我们请求的成功
    note.innerHTML += "<li>Request successful.</li>";
  };
}

规范

Specification
Indexed Database API 3.0
# ref-for-dom-idbobjectstore-indexnames①

浏览器兼容性

BCD tables only load in the browser

查看其他内容