我曾尝试研究类似的案例,但没有任何事情与我所经历的相符。请考虑以下内容:
var req = indexedDB.open("myDatabase", 1);
req.onupgradeneeded = function (evt) {
var objStore = evt.target.result.createObjectStore("customers", { keyPath: "ID", autoIncrement: true });
objStore.createIndex("fName", "fName", { unique: false });
objStore.createIndex("lName", "lName", { unique: false });
objStore.createIndex("country", "country", { unique: false });
var obj1 = { fName: "James", lName: "Smith", age: 25, country: "United States", reviewStatus: "pending" };
var obj2 = { fName: "Tim", lName: "Jones", age: 32, country: "Canada", reviewStatus: "pending"};
var obj3 = { fName: "James", lName: "Miller", age: 22, country: "United States", reviewStatus: "pending"};
var obj4 = { fName: "James", lName: "Thompson", age: 40, country: "United Kingdom", reviewStatus: "pending"};
var obj5 = { fName: "Joseph", lName: "Harris", age: 28, country: "Canada", reviewStatus: "pending"};
var obj6 = { fName: "James", lName: "Jackson", age: 30, country: "United States", reviewStatus: "pending"};
objStore.add(obj1);
objStore.add(obj2);
objStore.add(obj3);
objStore.add(obj4);
objStore.add(obj5);
objStore.add(obj6);
};
req.onsuccess = function (evt) {
var tx = this.result.transaction("customers", "readwrite");
var objStore = tx.objectStore("customers");
var index = objStore.index("fName");
var cnt = index.count(IDBKeyRange.only("James"));
cnt.onsuccess = function () {
console.log(cnt.result + " records with fName: James");
};
var cursor = index.openCursor(IDBKeyRange.only("James"));
cursor.onsuccess = function (e) {
var record = e.target.result;
if (record) {
var updateData = record.value;
updateData.reviewStatus = "reviewed";
//update some other fields
var updateReq = record.update(updateData);
console.log("updating record");
record.continue();
}
};
cursor.onerror = function () {
console.log("error using cursor");
};
tx.oncomplete = function () {
console.log("got to transaction oncomplete");
//do other work required once cursor updates done
};
tx.onerror = function () {
console.log("error in transaction");
};
};
req.onerror = function () {
console.log("error opening database");
};
我的数据库有4条记录,其中fName =“ James”。我使用Chrome(v71)首先对其进行测试并获得结果:
4条带有fName的记录:James
更新记录
更新 记录
更新记录
更新记录
交易未完成
随后在Firefox(v64),Opera(v56)和Chrome for Android上进行的测试均产生了这些预期结果。当我在iOS(11.4)上进行测试时,得到的结果是:
4条带有fName:James的记录
更新记录
交易未完成
我的onerror函数中没有任何控制台错误或任何消息。我无法弄清楚为什么在iOS中只更新第一条记录,而光标却没有移动到其他记录。我不知道我的代码中是否有错误,或者这是iOS中的某种错误?
答案 0 :(得分:1)
好像是Safari中的错误。我在这里https://bugs.webkit.org/show_bug.cgi?id=192987提交了一个错误报告-希望他们会在不久的将来对其进行修复!