javascript - 访问 IndexedDB 中的主键

标签 javascript local-storage indexeddb

我正在尝试将 IndexedDB 中的对象主键打印到控制台。

我见过两个使用“Cursor.Value.Key”的示例,但是当我使用它时,我得到“undefined”。

if (cursor) 
    {
        console.log(cursor.value.key);
        console.log(cursor.value.username);
        console.log(username)
        console.log(cursor.value.password);
        console.log(password)

这是我的数据库

objectStore = db.createObjectStore('users', { keyPath: "id", autoIncrement: true });
    objectStore.createIndex('username', 'username', { unique: true });
    objectStore.createIndex('password', 'password', {unique: false});

它应该打印对象的主键,即 1 或 2

最佳答案

尝试使用 cursor.key(或 cursor.primaryKey 作为索引而不是对象存储)或 cursor.value.idcursor.value 是对象本身,因此如果您将 keyPath 设置为“key”,cursor.value.key 只会返回主键。

关于javascript - 访问 IndexedDB 中的主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53961419/

相关文章:

iOS 应用程序迁移 - 将旧的 native 应用程序更新到 Cordova,迁移数据

html - 离线访问 - SQLite 或索引数据库?

javascript - 如果在分配时触发断点,IndexedDB 回调不会触发

javascript - 输入上的渲染功能消除了 Vue 中的 react 性

javascript - jQuery 使用动态添加的元素而不刷新

angularjs - 跨子域在angularjs中创建本地存储

android - 对于使用 Parse 后端的多设备应用程序,是否建议使用离线存储?

javascript - 为离线 Web 应用程序存储图像数据(客户端存储数据库)

javascript - 使用 jQuery 在预制/单独页面之间滑动

javascript - 单选按钮中未定义模板引用变量