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 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.
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.
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.
The HTTP DELETE request method deletes the specified resource. Request has body. May. Successful response has body. May. Safe. No. Idempotent. Yes.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With