Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete method is not working for Indexed DB HTML5... It returns success but the record is not deleted

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!

like image 387
the_moon Avatar asked Nov 19 '13 18:11

the_moon


People also ask

How to delete a database using IndexedDB deletedatabase?

The indexedDB deleteDatabase () method is used to request the deletion of a database. The method returns an IDBOpenDBRequest object immediately and performs the deletion operation asynchronously. name: The name of the database you want to delete. Return value: A IDBOpenDBRequest object after deletion.

What is HTML5 IndexedDB?

IndexedDB is a new feature of HTML5. Using IndexedDB we can store bulk data to the Client Browser. Know how to Store, Retrieve, Delete data from IndexedDB.

What is the status code for delete method?

Responses If a DELETE method is successfully applied, there are several response status codes possible: A 202 (Accepted) status code if the action will likely succeed but has not yet been enacted. A 204 (No Content) status code if the action has been enacted and no further information is to be supplied.

Is the HTTP delete request method idempotent?

The HTTP DELETE request method deletes the specified resource. Request has body. May. Successful response has body. May. Safe. No. Idempotent. Yes.


2 Answers

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.

like image 96
viridis Avatar answered Nov 15 '22 15:11

viridis


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.

like image 38
Kyaw Tun Avatar answered Nov 15 '22 14:11

Kyaw Tun