问题
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!
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/20078724/delete-method-is-not-working-for-indexed-db-html5-it-returns-success-but-the