我正在使用 IndexedDB,有两个对象仓库:equip(表示不同的设备,主键为tagNo),和equipParts(表示一件设备的部件,并且具有基于标记号/序列号的索引,主键seqNo,其中的一个字段tagNo表示该部件所属的设备)。
如果我删除equip中的记录,则希望删除所有在equipParts中具有equip的tagNo的记录(就像“where equipParts.tagNo = equip.tagNo”一样)。
来自我的代码摘录:
var tx = db.transaction(["equip", "equipParts"],"readwrite");
var estore = tx.objectStore("equip");
var pstore = tx.objectStore("equipParts");
var tagIndex = pstore.index("by_tagNo");
var pdestroy = tagIndex.openCursor(IDBKeyRange.only(tagno)); //opens all records bearing the selected tag number
pdestroy.onsuccess = function() {
var cursor = pdestroy.result;
if (cursor) {
if (cursor.value.tagNo == tagno) {
pstore.delete(cursor.value.seqNo); //I guess I'm wrong here
}
cursor.continue;
}
}
pdestroy.onerror = function() {
alert("Deletion attempt NG");
}
var ereq = estore.delete(tagno);
ereq.onsuccess = function(e) {
alert("Form deletion OK");
window.location = "index.html";
}
ereq.onerror = function(e) {
alert("Form deletion NG");
window.location = "index.html";
}
db.close();
问题在于仅删除了equip中的记录,而equipParts中的记录仍然存在。是否有一种方法可以根据非唯一索引(可以是父对象存储区的主键)删除IndexedDB对象存储区中的多个记录?
openCursor
返回IDBCursorWithValue
,需要读取记录值,而第一个返回IDBCursor
不包含记录值。 - Kyaw TunIDBObjectStore.delete()
进行删除。 - cleongonsuccess
回调函数,其中调用了continue
。 - Kyaw Tun