admin管理员组

文章数量:1392105

I am trying to implement this example.

Everything works fine until I attempt to delete a certain item.

Using this:

request.onupgradeneeded = function(event) {
      console.log("upgrade", event);

      db = event.target.result;
      console.log("db", db);

      if (!db.objectStoreNames.contains("chatBot")) {
        var objectStore = db.createObjectStore("chatBot", {keyPath: "timeStamp", autoIncrement: true});
      }
    };

and setting up the deletion:

btnDelete.addEventListener("click", function() {
      var id, transaction, objectStore, request;

      id = document.getElementById("txtID").value;
      console.log("id", typeof id);

      transaction = db.transaction("people", "readwrite");
      objectStore = transaction.objectStore("people");
      request = objectStore.delete(id);
      request.onsuccess = function(evt) {
        console.log("deleted content");
      };
    }, false);

There is no problem adding items to the indexedDB but for some reason I can't figure out why it can't delete the items.

The id is a string and the objectStore.delete(id) is the correct implementation.

Here is a pastebin of the example

Using Firefox 18

I am trying to implement this example.

Everything works fine until I attempt to delete a certain item.

Using this:

request.onupgradeneeded = function(event) {
      console.log("upgrade", event);

      db = event.target.result;
      console.log("db", db);

      if (!db.objectStoreNames.contains("chatBot")) {
        var objectStore = db.createObjectStore("chatBot", {keyPath: "timeStamp", autoIncrement: true});
      }
    };

and setting up the deletion:

btnDelete.addEventListener("click", function() {
      var id, transaction, objectStore, request;

      id = document.getElementById("txtID").value;
      console.log("id", typeof id);

      transaction = db.transaction("people", "readwrite");
      objectStore = transaction.objectStore("people");
      request = objectStore.delete(id);
      request.onsuccess = function(evt) {
        console.log("deleted content");
      };
    }, false);

There is no problem adding items to the indexedDB but for some reason I can't figure out why it can't delete the items.

The id is a string and the objectStore.delete(id) is the correct implementation.

Here is a pastebin of the example

Using Firefox 18

Share Improve this question edited Jan 15, 2013 at 4:05 user1460015 asked Jan 15, 2013 at 2:25 user1460015user1460015 2,0033 gold badges29 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Since you are using autoIncrement key, the key is generated by the user agent. In FF and Chrome, it is integer starting with 1. If you give valid key and convert your id to integer, your code run fine. I tested in both FF and Chrome (dartium). '1' and 1 are different keys according to IndexedDB API key definition.

Another issue IndexedDB API design. The delete methods always return to success event handler with undefined as result whether given key was deleted or not. So it is very difficult to debug. I think it should return number of deleted records at least.

[Edit] mod code: http://pastebin./mLpU0VfP
[Edit... also] Notice the + which converts the string to an integer
request = db.transaction("people", "readwrite").objectStore("people").delete(+id);

本文标签: javascriptDeleting item in IndexedDB while using autoIncrementStack Overflow