如何以相反的顺序获取 indexedDB 游标的结果
How to obtain results of an indexedDB cursor in reverse order
我编写了以下代码来迭代存储在 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 以获取一些文档。
我编写了以下代码来迭代存储在 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 以获取一些文档。