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①

浏览器兼容性

另见