gpt4 book ai didi

javascript - 从桌面 Chrome IndexedDB 删除记录与从 Android Chrome IndexedDB 删除记录

转载 作者:行者123 更新时间:2023-12-02 19:17:37 24 4
gpt4 key购买 nike

我正在尝试使用以下代码从 IndexedDB 中删除一条记录:

DB.indexedDB.IDBTransaction.READ_WRITE="readwrite";
window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;

if ('webkitIndexedDB' in window) {
window.IDBTransaction = window.webkitIDBTransaction;
window.IDBKeyRange = window.webkitIDBKeyRange;
DB.indexedDB.IDBTransaction.READ_WRITE=IDBTransaction.READ_WRITE;
}

DB.indexedDB.idelete = function( storeName, indexValue, index, keyPathfield ){
var db = DB.indexedDB.db;
var transaction = db.transaction([storeName], DB.indexedDB.IDBTransaction.READ_WRITE);
var store = transaction.objectStore(storeName);
var sindex = store.index(index);

sindex.get(indexValue).onsuccess = function (event){
store.delete(event.target.result[keyPathfield]).onsuccess = function(event) {
document.getElementById("result").innerHTML+="deleted<br>";
};
}
}

它调用onsuccess,但是当我添加具有相同indexValue的新记录并再次调用idelete并使用以下命令搜索记录时代码:

DB.indexedDB.readAll=function(storeName, index){
var results=document.getElementById("result");
var db = DB.indexedDB.db;
var transaction = db.transaction([storeName]);
var store = transaction.objectStore(storeName);
var key = IDBKeyRange.lowerBound(0);
var cursor = store.openCursor(key);
var x=0;

cursor.onsuccess = function(event){
var result = event.target.result;
if(result)
{
x++;
var charx=x.toString();
results.innerHTML+=charx+result.value[index]+"<br>";
result.continue();
}
else
return;
}

如果我使用 Windows Chrome,记录会被正确删除。但如果我使用的是 Android Chrome 版本 M18.1,readAll 仍然可以找到该记录,但 idelete 无法删除它,因为它实际上已被删除。

最佳答案

只打开键游标而不是实际检索它怎么样。

sindex.openKeyCursor(indexValue).onsuccess = function (event){
var cursor = event.target.result;
if (cursor) {
// cursor.delete();
var key = cursor.key;
var primaryKey = cursor.primaryKey;

store.delete(primaryKey).onsuccess = function(event) {
document.getElementById("result").innerHTML+= key + ' (' + primaryKey + ") deleted<br>";
};
}
} else {
document.getElementById("result").innerHTML+= indexValue + ' not found.<br>";
}
}

关于javascript - 从桌面 Chrome IndexedDB 删除记录与从 Android Chrome IndexedDB 删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12942581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com