【发布时间】:2018-12-31 07:30:37
【问题描述】:
TL;DR:collection.modify() 适用于几条记录,但在尝试一次修改超过 10 条记录时失败。为什么?
我有一些代码使用 Javascript、HTML 和 IndexedDB 上的 Dexie 包装器显示数据库并与之交互。问题是:我一次只能修改几条记录。如果我尝试超过 ~8-10 条记录,modify() 会失败且没有错误。
这是我调用数据库和绘制查询表的方式:
//Every time the page refreshes, the db is open and draws tables
$document.ready(function(){
//Open database
db=new Dexie("peopleManager");
db.version(1).stores({
people: "id++, name, location"
});
//Call functions that draw query tables
showDallas();
showNewYork();
});
//Draws an HTML table of people who work in Dallas
function showDallas(){
var output = '';
db.people
.where("location")
.equals("Dallas")
.each
(function(people){
output += "<tr align='center'>";
output += "<td>"+people.id+"</td>";
output += "<td>"+people.name+"</td>";
output += "</tr>";
$('#dallas').html(output);
});
}
//Draws an HTML table of people who work in NY
function showNewYork(){
var output = '';
db.people
.where("location")
.equals("New York")
.each
(function(people){
output += "<tr align='center'>";
output += "<td>"+people.id+"</td>";
output += "<td>"+people.name+"</td>";
output += "</tr>";
$('#newYork').html(output);
});
}
这是不断失败的功能。单击 HTML 按钮会触发它:
//Move all to New York
function moveToNewYork(){
db.transaction('rw', db.people, function(){
db.people.where("location").equals("Dallas").modify({location: "New York"});
}).then(function(){
window.location.reload();
}).catch(Dexie.ModifyError, function(e){
console.error(e.failures.length + "failed to hire modify");
throw e;
});
}
HTML 按钮:
<form role = "form" onSubmit = "moveToNewYork()">
<button type="submit" class="btn btn-primary">Move All</button></form>
我可以修改少于 10 条记录。超过十个,页面刷新,数据库没有变化,也没有错误记录。关注the documentation,但没有看到 modify() 需要更改 10 多条记录的任何示例。在一定数量的记录之后,我还没有发现任何显示 modify() 事务失败的东西。
任何人都知道我做错了什么,或者我可以如何进一步解决这个问题?
请注意,实际代码很长,还有大约 20 个其他只读操作正在进行。
更新:这是显示错误的full JSFiddle。有了这些(非常小的)记录,我可以在 modify() 开始失败之前达到 12-15。奇怪的是,再次随机单击几次会使 modify() 工作可能 8 次中的 1 次?我完全被难住了。
【问题讨论】:
-
您是否尝试过捕获所有错误而不仅仅是
Dexie.ModifyError?这可以为您指明正确的方向。.catch(function(e){ console.error(e); throw e; });编辑:啊,没关系,我没有仔细阅读以看到页面确实重新加载。 -
是的,无论如何添加 ModifyError 是在常规错误捕获从未记录任何内容之后的最新更改。
标签: javascript html indexeddb dexie