0

I have a store in IndexDB which called PendingFile

var pendingFileTable = db.createObjectStore("pendingfile", {keyPath: ['serverid', 'hash', 'segment']});

Now, i have 50 PendingFiles that they have the same (ServerId, Hash) and segment is from 1..50. Now i want to delete all of them by one transaction and one Delete operation using the IDBKeyRange.

So i do the following

export async function deleteByIds(collectionName, indexName, serverId, hash, totalSegments) {
    let request = new Promise((resolve) => {
        let myDatabase = indexedDB.open(DATABASE_NAME, CURRENT_VERSION);
        myDatabase.onsuccess = function () {
            let lower = [serverId, hash, 1];
            let upper = [serverId, hash, totalSegments];
            let range = IDBKeyRange.bound(lower, upper, false, false);
            let transaction = myDatabase.result.transaction(collectionName, 'readwrite');
            transaction.objectStore(collectionName).delete(range).onsuccess = (event) => {
                resolve(event);
            };
        }
    });
    let result = await request;
    return result;
}

However, after the function call i see that the PendingFile still has the entities on it. What do i do wrong?

james04
  • 1,580
  • 2
  • 20
  • 46
  • Resolve when the transaction completes, not when the request succeeds. Also, you need to reject when the transaction request errors, because if an error is happening, the promise never settles. – Josh Oct 15 '22 at 18:48

0 Answers0