IDBRequest:result 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

注意:此功能在 Web Workers 中可用。

resultIDBRequest 介面的一個只讀屬性,它返回請求的結果。

任意

異常

InvalidStateError DOMException

如果請求尚未完成,無法訪問結果,則嘗試訪問此屬性時會丟擲錯誤。

示例

以下示例請求一個給定的記錄標題,onsuccess 事件處理程式從 IDBObjectStore(透過 objectStoreTitleRequest.result 獲取)獲取關聯記錄,更新記錄的一個屬性,然後將更新後的記錄重新存入物件儲存。有關完整的可執行示例,請參閱我們的 待辦事項通知 應用(即時檢視示例)。

js
const title = "Walk dog";

// Open up a transaction as usual
const objectStore = db
  .transaction(["toDoList"], "readwrite")
  .objectStore("toDoList");

// Get the to-do list object that has this title as its title
const objectStoreTitleRequest = objectStore.get(title);

objectStoreTitleRequest.onsuccess = () => {
  // Grab the data object returned as the result
  const data = objectStoreTitleRequest.result;

  // Update the notified value in the object to "yes"
  data.notified = "yes";

  // Create another request that inserts the item
  // back into the database
  const updateTitleRequest = objectStore.put(data);

  // When this new request succeeds, run the displayData()
  // function again to update the display
  updateTitleRequest.onsuccess = () => {
    displayData();
  };
};

規範

規範
Indexed Database API 3.0
# ref-for-dom-idbrequest-result①

瀏覽器相容性

另見