我已经处理indexedDB Web存储一个星期了,这个问题仍然存在。代码仍然返回未定义的db,我需要request.onsuccess之外的db。
let db;
async function readCart() {
let request = window.indexedDB.open("cart", 1);
request.onsuccess = (success) => {
db = request.result;
}
request.onerror = error => {
console.error("Couldn't open database : ", error);
};
request.onupgradeneeded = event => {
let db = event.target.result;
//to create object store
let objectStore = db.createObjectStore("cart", {
keyPath: "id"
});
console.log("object store : ", objectStore);
};
console.log("Db is : ", await db); // this gives undefined
}