--- title: 'IDBRequest: success event' slug: Web/API/IDBRequest/success_event translation_of: Web/API/IDBRequest/success_event ---
successイベントはIDBRequestが成功すると着火します。
| Bubbles | No |
|---|---|
| Cancelable | No |
| Interface | {{domxref("Event")}} |
| Event handler property | onsuccess |
この例では、データベースをオープンします。そのsuccessイベントをaddEventListener()でリスンします。
// データベースをオープンする
const openRequest = window.indexedDB.open('toDoList', 4);
openRequest.onupgradeneeded = (event) => {
const db = event.target.result;
db.onerror = () => {
console.log('Error creating database');
};
// オブジェクトストアを作成する
var objectStore = db.createObjectStore('toDoList', { keyPath: 'taskTitle' });
// オブジェクトストアが保有するデータを定義する
objectStore.createIndex('hours', 'hours', { unique: false });
objectStore.createIndex('minutes', 'minutes', { unique: false });
objectStore.createIndex('day', 'day', { unique: false });
objectStore.createIndex('month', 'month', { unique: false });
objectStore.createIndex('year', 'year', { unique: false });
};
openRequest.addEventListener('success', (event) => {
console.log('Database opened successfully!');
});
下記は同じことをonsuccessイベントハンドラープロパティを使用した例です。
// データベースをオープンする
const openRequest = window.indexedDB.open('toDoList', 4);
openRequest.onupgradeneeded = (event) => {
const db = event.target.result;
db.onerror = () => {
console.log('Error creating database');
};
// オブジェクトストアを作成する
var objectStore = db.createObjectStore('toDoList', { keyPath: 'taskTitle' });
// オブジェクトストアが保有するデータを定義する
objectStore.createIndex('hours', 'hours', { unique: false });
objectStore.createIndex('minutes', 'minutes', { unique: false });
objectStore.createIndex('day', 'day', { unique: false });
objectStore.createIndex('month', 'month', { unique: false });
objectStore.createIndex('year', 'year', { unique: false });
};
openRequest.onsuccess = (event) => {
console.log('Database opened successfully!');
};
{{Compat("api.IDBRequest.success_event")}}
onsuccess event handler property