javascript - IndexedDB的IDBCursor中key和primaryKey有什么区别

标签 javascript indexeddb

documentation here says that

Returns the cursor's current key. (Cursors also have a key and a value which represent the key and the value of the last iterated record.)

主键

Returns the cursor's current effective key. (If the source of a cursor is an object store, the effective object store of the cursor is that object store and the effective key of the cursor is the cursor's position.)

但是在下面的示例中,两者的使用完全相同,我得到的值也相同:

那么实际区别是什么?

最佳答案

如果您遍历对象存储,它们是相同的。

如果您正在遍历索引,索引键primaryKey对象中的键商店

例如:

 book_store = db.createObjectStore('books');
 title_index = store.createIndex('by_title', 'title');

 key = 123;
 value = {title: 'IDB for Newbies', author: 'Alice'};
 book_store.put(value, key);

 book_store.openCursor().onsuccess = function(e) {
   cursor = e.target.result;
   console.log(cursor.key); // logs 123
   console.log(cursor.primaryKey); // logs 123
 };
 title_index.openCursor().onsuccess = function(e) {
   cursor = e.target.result;
   console.log(cursor.key); // logs 'IDB for Newbies'
   console.log(cursor.primaryKey); // logs 123
 };

关于javascript - IndexedDB的IDBCursor中key和primaryKey有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37971117/

相关文章:

javascript - 如何在 IndexedDB 中进行 JOIN 类型查询

javascript - 选项卡关闭时清理 IndexedDB

flutter - 如何在 Flutter 中导入 Dart IndexedDB 库?

php - HTML、CSS、JAVASCRIPT、PHP——把它们放在一起

javascript - 如何从 jszip 和 angularjs 中的 zipobject 获取 csv 文件对象

javascript - 仅使用 javascript 清除 DIV 中的所有文本字段

javascript - Javascript 中带有数字点的模式验证

javascript - 需要自定义模块的问题

indexeddb - IndexedDb 文件位置可以是共享位置吗

javascript - YDN-DB 可以一次处理大条目吗?