值
一个包含对象存储名称的字符串。
异常
当您尝试更改对象存储的名称时,可能会出现几种异常。
InvalidStateErrorDOMException-
如果在对象存储已被删除,或者当前事务不是升级事务,则会抛出此异常;您只能在升级事务期间重命名索引;也就是说,当模式为
versionchange时。 TransactionInactiveErrorDOMException-
如果当前事务未激活,则会抛出此异常。
ConstraintErrorDOMException-
如果对象存储已在使用指定的
name,则会抛出此异常。
示例
在下面的代码片段中,我们打开数据库的读/写事务,并使用 add() 向对象存储添加一些数据。创建对象存储后,我们将 objectStore.name 记录到控制台。完整的可运行示例,请参阅我们的 待办事项通知 应用(实时查看示例)。
js
// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
DBOpenRequest.onsuccess = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Database initialized.";
// store the result of opening the database in the db variable.
// This is used a lot below
db = DBOpenRequest.result;
// Run the addData() function to add the data to the database
addData();
};
function addData() {
// Create a new object ready to insert into the IDB
const newItem = [
{
taskTitle: "Walk dog",
hours: 19,
minutes: 30,
day: 24,
month: "December",
year: 2013,
notified: "no",
},
];
// open a read/write db transaction, ready for adding the data
const transaction = db.transaction(["toDoList"], "readwrite");
// report on the success of the transaction completing, when everything is done
transaction.oncomplete = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Transaction completed.";
};
transaction.onerror = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Transaction not opened due to error. Duplicate items not allowed.";
};
// create an object store on the transaction
const objectStore = transaction.objectStore("toDoList");
console.log(objectStore.name);
// Make a request to add our newItem object to the object store
const objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = (event) => {
// report the success of our request
note.appendChild(document.createElement("li")).textContent =
"Request successful.";
};
}
规范
| 规范 |
|---|
| Indexed Database API 3.0 # ref-for-dom-idbobjectstore-name① |
浏览器兼容性
加载中…
另见
- 使用 IndexedDB
- 开始事务:
IDBDatabase - 使用事务:
IDBTransaction - 设置键的范围:
IDBKeyRange - 检索和修改数据:
IDBObjectStore - 使用游标:
IDBCursor - 参考示例:待办事项通知(查看实时示例)。