IndexedDB如何为事务创建方法

0vvn1miw  于 12个月前  发布在  IndexedDB
关注(0)|答案(1)|浏览(127)

我想为事务创建一个类方法,当我试图让一个objectstore使用它时,得到一个错误:未捕获的类型错误:transaction.objectStore不是IndexedDB.saveToDataBase中的函数
类方法获取值storeName,并且它在console中可见。log(storeName)= reminderList

saveToDataBase(object, id) {        //storeName - name of the storage in the database,
                                        //object - the object being saved,
                                        //id - id of the object being saved
        console.log(object, id)
        console.log(storeName)
        console.log(database)

        // new transaction
        const transaction = this._makeTransaction(storeName, 'readwrite');
        const store = transaction.objectStore(storeName);   // an error occurs here!
        store.put(object, id);  // write record
        console.log(transaction)

    };

    _makeTransaction(storeName, mode) {
        return new Promise((resolve, reject) => {
            let transaction = database.transaction(storeName, mode)
            transaction.oncomplete = () => {
                resolve(transaction); // success
                console.log(transaction)
            };

            transaction.onerror = () => {
                reject(transaction.error); // failure
            };
        })

    };

我是这样做的,它可以工作,但我不明白为什么它在事务的类方法中不起作用:

saveToDataBase(object, id) {        //storeName - name of the storage in the database,
                                        //object - the object being saved,
                                        //id - id of the object being saved
                                        console.log(object, id)
                                        console.log(storeName)
                                        console.log(database)
        return new Promise((resolve, reject) => {

            // new transaction
            const transaction = this._makeTransaction(storeName, 'readwrite');
            const store = transaction.objectStore(storeName);

            // write record
            store.put(object, id);  //

            transaction.oncomplete = () => {
                resolve(true); // success
            };

            transaction.onerror = () => {
                reject(transaction.error); // failure
            };

        })
    };

    _makeTransaction(storeName, mode) {
        let transaction = database.transaction(storeName, mode)
        return transaction
    }
fdbelqdn

fdbelqdn1#

top _makeTransaction函数返回一个promise,而不是一个transaction。底部的_makeTransaction函数返回一个事务。
当然,promise没有.objectStore函数。

相关问题