【问题标题】:Getting the id of an object added to an indexedDB object store获取添加到 indexedDB 对象存储的对象的 id
【发布时间】:2012-11-09 16:13:31
【问题描述】:

给定一个 indexeddb 数据库,其中一个声明的对象存储为:

var objectStore = thisDb.createObjectStore("table", {keyPath: "id", autoIncrement: true})

当我使用添加请求添加新项目时:

var resp = objectStore.add(row)

如何在onsuccess函数中获取这个添加项的新id?

resp.onsuccess = function(e) { /* code to get the id of the added item */}

【问题讨论】:

    标签: indexeddb


    【解决方案1】:

    您可以在 e.target.result 中找到插入的密钥。

    【讨论】:

    • 是否有说明add() 方法返回什么的文档?
    • add 方法返回一个 IDBRequest (w3.org/TR/IndexedDB/…) 对象。但是我认为您不会对此了解更多:)。如果您看一下存储对象的步骤(w3.org/TR/IndexedDB/…),最后您会看到 IDBRequest 对象的结果设置为键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    相关资源
    最近更新 更多