我编写了以下代码来迭代存储在indexedDB
. 我正在使用 Google Chrome 浏览器。
'use strict';
var openRequest = indexedDB.open('Library', 1);
var db;
openRequest.onupgradeneeded = function(response)
{
console.debug(1);
response.currentTarget.result.createObjectStore("authors",{ keypath: 'id', autoIncrement: true });
}
openRequest.onsuccess = function(response) {
console.debug('success opening indexeddb');
db = openRequest.result;
findAuthors();
};
function findAuthors() {
var trans = db.transaction('authors', 'readonly');
var authors = trans.objectStore("authors");
var request = authors.openCursor();
request.PREV = true;
request.onsuccess = function(response) {
var cursor = response.target.result;
if (!cursor) {
alert('No records found.');
return;
}
alert('Id: ' + cursor.key + ' Last name: ' + cursor.value.lastName);
cursor.continue();
};
request.onerror = function(response) { // display error
};
}
我的数据库中的记录如下:
目前,迭代按键 2,3 和 4 的顺序发生。我想要的是当我开始迭代游标时,我得到带有键 4,然后是 3,然后是 2 的行,即以它们插入的相反顺序。我尝试PREV
在游标对象上使用布尔属性,但它似乎不起作用:
request.PREV = true;
尝试 authors.openCursor(null, 'prev');
另请查看https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/openCursor以获取一些文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句