IDBRequest:result 属性

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

resultIDBRequest 接口的只读属性,它返回请求的结果。如果请求未完成,则结果不可用,并且会抛出 InvalidStateError 异常。

任意

示例

以下示例请求给定的记录标题 onsuccessIDBObjectStore(作为 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①

浏览器兼容性

BCD 表格仅在启用 JavaScript 的浏览器中加载。

另请参阅