我编写了以下代码来迭代存储在 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
bool 属性,但它似乎不起作用:
request.PREV = true;
最佳答案
尝试authors.openCursor(null, 'prev');
另请查看https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/openCursor一些文档。
关于javascript - 如何以相反的顺序获取indexedDB游标的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44360910/