使用 IndexedDB 时,如何使用不是键的索引删除多条记录?

When using IndexedDB, how can I delete multiple records using an index that is not the key?

我这里有创建索引数据库的代码:

function create_db() {
    var indexedDB = window.indexedDB || window.webkitIndexedDB || window.msIndexedDB;
    var request = indexedDB.open(“photos”, 2);

    request.onupgradeneeded = function(event) {
        var db = event.target.result;

        // Create photo db
        var photo_store = db.createObjectStore("photos", {keyPath: "photo_id"});
        var photo_id_index = photo_store.createIndex("by_photo_id",        "photo_id", {unique: true});
        var dest_id_index  = photo_store.createIndex("by_destination_id",  "destination_id");

        console.log(“store created”);
    };

    request.onsuccess = function(event) {
        console.log(“store opened”);
    };

    request.onerror = function(event) {
        console.log("error: " + event);
    };

}

我删除条目的代码:

 function remove_photos = function (destination_id, db) {
var transaction = db.transaction("photos", "readwrite");
var store       = transaction.objectStore("photos");
var index       = store.index("by_destination_id");
var request     = index.openCursor(IDBKeyRange.only(destination_id));

request.onsuccess = function() {
    var cursor = request.result;

    if (cursor) {
        cursor.delete();
        cursor.continue();
    }
};

}

如何删除使用 by_destination_id 索引的记录,以便删除具有给定 destination_id(整数)的所有记录?

感谢您的帮助。

我找到了我的问题的解决方案,IDBKeyRange.only 函数不喜欢整数,它需要是一个字符串,所以将这一行替换为:

var request = index.openCursor(IDBKeyRange.only(destination_id.toString()));

使代码正常工作。