Javascript IndexedDB使用键获取所有数据
使用IndexedDB API,我们有以下两种方法:Javascript IndexedDB使用键获取所有数据,javascript,asynchronous,indexeddb,Javascript,Asynchronous,Indexeddb,使用IndexedDB API,我们有以下两种方法:getAll()和getAllKeys(),使用示例如下: let transaction=this.db.transaction([“表”]); 让object_store=transaction.objectStore(“表”); request=object_store.getAll();/*或getAllKeys()*/ request.onerror=(事件)=>{ console.err(“获取数据时出错”); }; request
getAll()
和getAllKeys()
,使用示例如下:
let transaction=this.db.transaction([“表”]);
让object_store=transaction.objectStore(“表”);
request=object_store.getAll();/*或getAllKeys()*/
request.onerror=(事件)=>{
console.err(“获取数据时出错”);
};
request.onsuccess=(事件)=>{
console.log(request.result);
};
问题是getAll()
似乎只检索数组格式的数据,而getAllKeys()
则获取没有数据的所有键。我找不到同时获取键和值的方法
我能够通过一个回调函数使用如下方法检索所有值及其键:
transaction=this.db.transaction([“表”]);
object_store=transaction.objectStore(“表”);
request=object_store.openCursor();
request.onerror=函数(事件){
console.err(“获取数据时出错”);
};
request.onsuccess=函数(事件){
让cursor=event.target.result;
如果(光标){
让key=cursor.primaryKey;
设value=cursor.value;
console.log(键、值);
cursor.continue();
}
否则{
//没有更多的结果
}
};
嗯,我真的不知道该如何理解这个问题。不太清楚,自己回答也不清楚。也不知道为什么它会被否决。但是你可以使用getAll,只需访问每个对象的属性…@Josh在找到问题的解决方案后,我回答了自己。澄清一下:确保可以使用getAll()访问所有对象的属性,但不能访问存储在IndexedDB表中的对象键。表存储器将内容存储为key:object
对的列表。getAll()只给我对象,getAllKeys()只给我键。我想要一个方法来检索我的键和对象(或数据),而不需要多次异步调用。使用内联键和getAllJust遇到了这个问题,发现Mechanical的解决方案非常适合。内联键是一种相当糟糕的做法,它是数据复制,需要不断保持同步。