需要帮助以了解如何访问 INDEXEDDB 请求结果数组的返回结果
Need help to understand how to access the returned results of an INDEXEDDB request result array
我是 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) 正确执行?
干杯
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);
}
});
};
我是 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) 正确执行? 干杯
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);
}
});
};