【问题标题】:Accessing the primary key in IndexedDB访问 IndexedDB 中的主键
【发布时间】:2019-05-26 10:47:25
【问题描述】:

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

我见过两个使用“Cursor.Value.Key”的例子,但是当我使用它时,我得到了“未定义”。

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

【问题讨论】:

    标签: javascript local-storage indexeddb


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 2021-01-20
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      相关资源
      最近更新 更多