files/en-us/web/api/idbobjectstore/opencursor/index.md
{{ APIRef("IndexedDB") }} {{AvailableInWorkers}}
The openCursor() method of the
{{domxref("IDBObjectStore")}} interface returns an {{domxref("IDBRequest")}} object,
and, in a separate thread, returns a new {{domxref("IDBCursorWithValue")}} object.
Used for iterating through an object store with a cursor.
openCursor()
openCursor(query)
openCursor(query, direction)
query {{optional_inline}}
direction {{optional_inline}}
next. Valid values are:
next
nextunique
prev
prevunique
An {{domxref("IDBRequest")}} object on which subsequent events related to this operation are fired.
If the operation is successful, the value of the request's {{domxref("IDBRequest.result", "result")}} property is:
null if no matching records were found.This method may raise a {{domxref("DOMException")}} of one of the following types:
InvalidStateError {{domxref("DOMException")}}
TransactionInactiveError {{domxref("DOMException")}}
DataError {{domxref("DOMException")}}
In this simple fragment we create a transaction, retrieve an object store, then use a cursor to iterate through all the records in the object store:
const transaction = db.transaction("name", "readonly");
const objectStore = transaction.objectStore("name");
const request = objectStore.openCursor();
request.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
// cursor.value contains the current record being iterated through
// this is where you'd do something with the result
cursor.continue();
} else {
// no more results
}
};
{{Specifications}}
{{Compat}}