javascript - What's the difference between key and primaryKey in IDBCursor of IndexedDB - Stack Overflow

The documentation here says thatkeyReturns the cursor's current key. (Cursors also have a key and

The documentation here says that

key

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.)

primaryKey

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.)

In the examples below however the two are used exactly the same and I get the same values for both:

So what is the practical difference?

The documentation here says that

key

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.)

primaryKey

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.)

In the examples below however the two are used exactly the same and I get the same values for both:

  • https://developer.mozilla/en-US/docs/Web/API/IDBCursor/primaryKey
  • https://developer.mozilla/en-US/docs/Web/API/IDBCursor/key

So what is the practical difference?

Share Improve this question edited Jun 22, 2016 at 14:47 uberlaufer asked Jun 22, 2016 at 14:31 uberlauferuberlaufer 2612 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

If you're iterating over an object store, they are the same.

If you are iterating over an index, the key is the index key and the primaryKey is the key in the object store.

For example:

 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
 };

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745478898a4629473.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信