需要帮助以了解如何访问INDEXEDDB请求结果数组的返回结果

fquxozlt  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(1)|浏览(173)

我是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)正确地完成它呢?干杯

brqmpdu1

brqmpdu11#

getAll会产生数组结果。例如:

function query(db, myCallbackFunction) {
  const tx = db.transaction('mystore');
  const store = tx.objectStore('mystore');
  const request = store.getAll();

  request.onsuccess = event => {
    // denote the array of objects with a variable
    // here, event.target is === to request, can use either one
    const data = event.target.result;
    // pass the data to the callback function so that caller can
    // access it
    myCallbackFunction(data);
  };
}

// Open the database and then run the query
var openRequest = indexedDB.open('mydb');
openRequest.onsuccess = event => {
  query(db, (data = []) => {
    // This gets called when the query has run with the loaded
    // data
    console.log('received %d rows of data', data.length);
    for (const row of data) {
      console.log('object:', row);
    }
  });
};

相关问题