具有异步/等待的IndexedDB

时间:2019-07-18 12:09:53

标签: javascript indexeddb async.js

我已经处理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

}

0 个答案:

没有答案