IDBObjectStore.get

This article is in need of a technical review.

« IDBObjectStore

The IDBObjectStore.get method returns an IDBRequest object, and, in a separate thread, returns the object store selected by the specified key.

If a value is successfully found, then a structured clone of it is created and set as the result of the request object.

Note: This method produces the same result for: a) a record that doesn't exist in the database and b) a record that has an undefined value. To tell these situations apart, call the openCursor() method with the same key. That method provides a cursor if the record exists, and no cursor if it does not.

Syntax

IDBRequest get (any key);

Example

// Let us open our database
var request = window.indexedDB.open("toDoList", 4);
    
// store the result of opening the database in the db variable.
db = request.result;

// Open a transaction on the current database
var transaction = db.transaction(["toDoList"]);
var objectStore = transaction.objectStore("toDoList");

// Use get() to get a specific object from the object store, the key of which is "Walk dog"
var request = objectStore.get("Walk dog");
request.onerror = function(event) {
  console.log("There is no record stored for " + request.result.taskTitle);
};
request.onsuccess = function(event) {
  // Do something with the request.result!
  console.log("The deadline time for " + request.result.taskTitle + " is " +
              request.result.hours + ":" + request.result.minutes + ".";
};

Parameters

key
The key or key range that identifies the record to be retrieved.

Returns

IDBRequest
A request object on which subsequent events related to this operation are fired.

Exceptions

This method may raise a DOMException with a DOMError of the following types:

Exception Description
TransactionInactiveError This IDBObjectStore's transaction is inactive.
DataError

The key or key range provided contains an invalid key.

InvalidStateError The IDBObjectStore has been deleted or removed.

Specifications

Specification Status Comment
Indexed Database API Candidate Recommendation  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 12 4.0 (2.0) 10 17 Not supported
count() 23 10.0 (10.0) 10 17 Not supported
Feature Android Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support 4.4 6.0 (6.0) 1.0.1 10 17 Not supported
count() 4.4 ? 1.0.1 10 17 Not supported

Be careful in Chrome as it still implements the old specification along the new one. Similarly it still has the prefixed webkitIndexedDB property even if the unprefixed indexedDB is present.

See also

  • Using IndexedDB
  • The reference application for the examples in this reference: To-do Notifications (view example live.) Not every snippet appears in this example, but every example uses the same data structure and syntax, and they will make sense in the context of this example.

Document Tags and Contributors

Contributors to this page: chrisdavidmills
Last updated by: chrisdavidmills,