使用indexedDB进行筛选时遇到问题。
我有用户与名称aaa,bbb,ccc和索引是对名称。
这是我的代码,无论我对游标应用哪个IDBKeyRange过滤器,我都得到空列表。我尝试使用bound和lowerBound。
const usersData = [
{ id: '1', name: 'bbb', email: 'bbb@somewhere.com' },
{ id: '2', name: 'aaa', email: 'aaa@somewhere.com' },
{ id: '3', name: 'ccc', email: 'ccc@somewhere.com' },
];
let db;
let request = window.indexedDB.open('test1', 1);
request.onerror = function(e) {
console.error(e);
};
request.onsuccess = function(e) {
db = e.target.result;
console.log('success, db: ', db);
};
request.onupgradeneeded = function(e) {
db = e.target.result;
let objectStore = db.createObjectStore('user', { keyPath: 'id' });
for (let user of usersData) {
objectStore.add(user);
}
objectStore.createIndex('name', 'name');
}
function readAll() {
let objectStore = db.transaction(['user'])
.objectStore('user');
let range = null;
//range = IDBKeyRange.lowerBound('aaa', true); // ? doesn't working
range = IDBKeyRange.bound(['aaa'], ['ccc'], true, true);
objectStore.openCursor(range).onsuccess = function(e) {
let cursor = e.target.result;
if (cursor) {
console.log(cursor.key, cursor.value);
cursor.continue();
}
else {
console.log('---------------------------------');
}
};
}
1条答案
按热度按时间hm2xizp91#
更改代码,以便在索引上打开游标,而不是在对象存储区上。