我是INDEXEDDB的新手,我有一个getAll方法在我的对象存储上工作,但是我不知道如何访问我返回的结果。
这是我的程式码,从表单上的'click'事件侦听程式呼叫:
var request = indexedDB.open(DB_NAME, DB_VERSION);
request.onupgradeneeded = function() {
// Create a new object store if this is the first time we're using
// this DB_NAME/DB_VERSION combo.
//request.result.createObjectStore(STORE_NAME, {autoIncrement: true});
};
request.onsuccess = function() {
db = request.result;
console.log('Database opened!');
var transaction = db.transaction(STORE_NAME, 'readonly');
var objectStore = transaction.objectStore(STORE_NAME);
if ('getAll' in objectStore) {
// IDBObjectStore.getAll() will return the full set of items in our store.
objectStore.getAll().onsuccess = function(event) {
console.log("getAll Success");
// .gList is an unordered list on my PHP page that i am attempting
// to populate with the returned values.
let list = document.querySelector('.gList');
list.innerHTML = `<li>Loading...</li>`;
// This is successful and returns the data that i expect as displayed below.
let request = event.target;
// This shows the results of my request
console.log({request});
// this returns undefined. I've tried all manner of ways of
// referencing the array that is inside this returned IDBRequest object
// and that is what i cannot figure out.
**console.log('request attr ' + request.team1Name);**
返回的IDBRequest对象的顶级结果如下所示:
{request: IDBRequest}
request: IDBRequest
error: null
onerror: null
onsuccess: ƒ (event)
readyState: "done"
result: Array(1)
0: Array(1)
0: {gameDate: "2021-08-29", gameTime: "12:38 PM", team1Name: "TeamOne", team2Name: "TeamTwo", gameCompleted: false, …}
gameID: 1
length: 1
[[Prototype]]: Array(0)
length: 1
[[Prototype]]: Array(0)
source: IDBObjectStore {name: "gamesList", keyPath: "gameID", indexNames: DOMStringList, transaction: IDBTransaction, autoIncrement: true}
transaction: IDBTransaction {objectStoreNames:
etc......
很明显,我已经得到了想要的结果,但是我不知道如何通过IDBRequest对象来获取数据。我该如何(a)获取结果数组值,(b)正确地完成它呢?干杯
1条答案
按热度按时间brqmpdu11#
getAll
会产生数组结果。例如: