Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

javascript - I wanted to add objects stored in indexDB to an array by reading with a cursor object

I wanted to store objects from indexDB into an array. But I cant access to objects. It always results in undefined. I even tried converting to JSON before storing on indexDB and parsing when retrieving. Nothing seems to work.

  function getFromDB() {
    // Open
    let openRequest = indexedDB.open("TestDB", 1);
    let db;
    let x = [];
    openRequest.onerror = function () {
      console.log("Error Opening DB");
    };
    openRequest.onsuccess = function () {
      db = openRequest.result;
      let readRequest = db
        .transaction(["Jobs"], "readwrite")
        .objectStore("Jobs")
        .openCursor();

      readRequest.onerror = function () {
        console.log("Error Reading");
      };

      readRequest.onsuccess = function (e) {
        let cursor = e.target.result;
        if (cursor) {
          x.push(cursor.value);

          cursor.continue();
        }
      };
    };
    console.log(x);
  }
  getFromDB();
});
question from:https://stackoverflow.com/questions/66052740/i-wanted-to-add-objects-stored-in-indexdb-to-an-array-by-reading-with-a-cursor-o

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...