admin管理员组文章数量:1426794
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 badges1 Answer
Reset to default 10If 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
};
本文标签: javascriptWhat39s the difference between key and primaryKey in IDBCursor of IndexedDBStack Overflow
版权声明:本文标题:javascript - What's the difference between key and primaryKey in IDBCursor of IndexedDB - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745478898a2660089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论