尝试使用indexeddb中的索引查询删除数据时出错:index.delete不是函数

jqjz2hbq  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(1)|浏览(214)

我想删除带索引的数据,但我得到错误。如果我直接删除没有索引的对象存储,没有问题,但我需要的是删除带索引。
Database screenshot

创建数据库函数

var objectStore = db.createObjectStore("rooms", { keyPath: "id", autoIncrement: true });
        let index = objectStore.createIndex('floorIndex', ['floorIndex']);

删除函数

var floorIndexValue = "2";
    const request = window.indexedDB.open("AdminDatabase");

    request.onsuccess = (event) => {
      const db = event.target.result;


      const txnRoom = db.transaction('rooms', 'readwrite');
      const storeRoom = txnRoom.objectStore('rooms');

   
      const index = storeRoom.index('floorIndex');

      let query = index.delete([floorIndexValue]);

      query.onerror = function (event) {
        console.log(" Room Delete Error DB: " + event);

      };
      query.onsuccess = function (event) {
        console.log(" Room Delete Succes DB: " + event);

      };
    };
eanckbw9

eanckbw91#

我在这里解决了IDBKeyRange的问题:

const keyRangeValue = IDBKeyRange.only([getRoomButtonNumber]);

      var transaction = db.transaction("rooms", "readwrite");
      var store = transaction.objectStore("rooms");
      var index = store.index("floorIndex");
      var request = index.openCursor(keyRangeValue);

      request.onsuccess = function (event) {

        var cursor = event.target.result;
        console.log("Room Delete Succes DB: " + cursor);

        if (cursor) {
          console.log("cursor is not null");
          cursor.delete();
          cursor.continue();
        }
      };

相关问题