IDBObjectStore.get()

翻译不完整。 请帮助我们翻译这篇文章!

IDBObjectStore 的接口 get()方法 返回 IDBRequest 对象,并在“单独的线程(separate thread)”中返回由指定键选择的“对象储存(object store)” 。这用于从对象储存检索特定记录。

如果成功找到值,则会创建其值的结构化克隆,并设置为“请求对象(request object)”的 result

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.
Note: 此特性在 Web Worker 中可用。

语法

var request = objectStore.get(key);

参数

key
标识要检索的记录的键或键范围

返回值

触发与此操作相关的后续事件的 IDBRequest 对象。

异常

此方法可能会引发以下类型之一的 DOMException :

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.
 

例子

在以下的代码段中,我们在数据库上打开一个“读/写 事务(read/write transaction)”,并使用 get() 从“对象储存( object store )”中获取一个特定的记录——一个带有“Walk dog”键的示例记录。一旦检索到这个数据对象,你就可以使用普通的JavaScript更新它,然后使用 IDBObjectStore.put 操作将其放回数据库。有关完整的工作示例,查看我们的 To-do Notifications app (view example live.)

// Let us open our database
var DBOpenRequest = window.indexedDB.open("toDoList", 4);

DBOpenRequest.onsuccess = function(event) {
  note.innerHTML += '<li>Database initialised.</li>';
    
  // store the result of opening the database in the db variable.
  // This is used a lot below
  db = DBOpenRequest.result;
    
  // Run the getData() function to get the data from the database
  getData();
};

function getData() {
  // open a read/write db transaction, ready for retrieving the data
  var transaction = db.transaction(["toDoList"], "readwrite");

  // report on the success of the transaction completing, when everything is done
  transaction.oncomplete = function(event) {
    note.innerHTML += '<li>Transaction completed.</li>';
  };

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

  // create an object store on the transaction
  var objectStore = transaction.objectStore("toDoList");

  // Make a request to get a record by key from the object store
  var objectStoreRequest = objectStore.get("Walk dog");

  objectStoreRequest.onsuccess = function(event) {
    // report the success of our request
    note.innerHTML += '<li>Request successful.</li>';

    var myRecord = objectStoreRequest.result;
  };

};

规范

Specification Status Comment
Indexed Database API 2.0
get()
Recommendation
Indexed Database API Draft
get()
Recommendation

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
getChrome Full support 24
Full support 24
No support 23 — 57
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge Full support 12Firefox Full support 16
Full support 16
No support 10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE Partial support 10Opera Full support 15Safari Full support 7WebView Android Full support Yes
Full support Yes
No support ? — 57
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Chrome Android Full support 25
Full support 25
No support 25 — 57
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android Full support 22Opera Android Full support 14Safari iOS Full support 8Samsung Internet Android Full support 1.5
Full support 1.5
No support 1.5 — 7.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit

Legend

Full support  
Full support
Partial support  
Partial support
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

另请参阅