10

Another problem that I am getting with Indexed DB for HTML5, using Desktop Chrome, is that I can not delete a record from an object store. The onsuccess event is triggered but the record is still there... My ID is a time stamp just because I wanted to achieve a working app faster. I hardcoded it but it still does not work. It is really strange because the onsuccess event is triggered...

The part of the code that "does it" is the following:

try {

        if (localDatabase != null && localDatabase.db != null) 
        {
            var store = localDatabase.db.transaction("patients", "readwrite").objectStore("patients");
            var request = store.delete("1384882073632");

            request.onsuccess = function(event) 
            {


                alert("Patient deleted from DB");
                update_patients_stored();

                aux1.value = "";
                aux2.value = "";
                aux3.value = "";
                aux4.value = "";

            };

            request.onerror = function(event) 
            {
                alert("Error deleting");
            };
        }
    }
    catch(e)
    {
        alert(e);
    }

Thank you in advance!

the_moon
  • 553
  • 1
  • 7
  • 21
  • The same than happens in my other question: http://stackoverflow.com/questions/20078335/do-i-need-to-refresh-a-page-to-see-if-the-indexed-db-was-reset – the_moon Nov 19 '13 at 18:12
  • I have changed the id and it worked but I can see results after refreshing the page. I have a button to update what is in the DB and show it but with this button I can still see the deleted one. – the_moon Nov 19 '13 at 18:15

3 Answers3

2

I've found a similar problem. Non-deleting behaviour but onsuccess event fired. Despite w3 IDBObjectStore.delete specification states key parameter can be of any type, I solved it forcing the parameter conversion to number:

//My version
//I use default autoIncrement for keys
var objectStore = db.createObjectStore(objStoreName,{autoIncrement:true});
//appears to need a number for the key (but retrieved from HTML as string
var request = db.transaction(objStoreName,'readwrite')
                    .objectStore(objStoreName)
                    .delete(Number(parent.id));
    request.onsuccess = function(e) {
        console.log("element deleted"); //sadly this always run :-(
    }

So your line four should be:

var request = store.delete(1384882073632); //integer id
//rather than (WRONG):
//var request = store.delete("1384882073632"); //WRONG! string id

Tried it on:

Chromium -- Version 50.0.2661.94 (64-bit) on Manjaro Linux

Have not tested it on other environments or browsers, but guess that's your problem.

viridis
  • 177
  • 10
1

I am sure you are deleting to wrong key. Note that number, string and date are different keys even if they are same if you compare with ==.

IndexedDB return success as long as it does not violate database constraint. In this case not deleting any record. I have propose to return number of deleted record on delete request, but not favored. My library, ydn-db, did it, of course.

Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
  • No, that's the correct key. I have checked it using the Developer Tools of Chrome... The weird thing is that when I refresh/reload the page, I can see that the record was deleted. It was deleted but I can only see that it was deleted after refreshing the website. – the_moon Nov 20 '13 at 13:13
  • I got it! The problem was that I need to use the oncomplete event to update the UI... Also, I have a minor problem in my update function. Thank you! – the_moon Nov 20 '13 at 18:13
  • I solved this issue by converting delete function parameter to number by Number() function. – Ehsan Chavoshi Mar 01 '21 at 19:43
1

Try putting key in an Array.

type IDBValidKey = number | string | Date | BufferSource | IDBArrayKey;

interface IDBArrayKey extends Array<IDBValidKey> {}

Ex.

dbRequest.onsuccess = function (event: any) {
      const db: IDBDatabase = event.target.result;

      const transaction = db.transaction([albumStoreObjName], "readwrite");

      transaction.oncomplete = () => {
        subscriber.next();
        subscriber.complete();
      };

      const objectStore = transaction.objectStore(albumStoreObjName);
      const request = objectStore.delete([albumId]);

      // ...
    };
Chris Hermut
  • 1,708
  • 3
  • 17
  • 32
  • 1
    wow! i use a guid as a key and your solution work well in my case. Have no idea why... really! – xentia Aug 10 '22 at 10:03