【发布时间】:2015-05-06 20:30:33
【问题描述】:
我希望有人有一些想法。这给我带来了一些真正的问题:
Uncaught (in promise) DOMException: Failed to execute 'transaction' on 'IDBDatabase': The database connection is closing. {message: "Failed to execute 'transaction' on 'IDBDatabase': The database connection is closing.", name: "InvalidStateError", code: 11, stack: "Error: Failed to execute 'transaction' on 'IDBData…ss (http://x.example.com/jav.js:352:56)", INDEX_SIZE_ERR: 1…}n.openTransactionSafely @ pouchdb-3.3.1.min.js:7e._get @ pouchdb-3.3.1.min.js:8(anonymous function) @ pouchdb-3.3.1.min.js:7(anonymous function) @ pouchdb-3.3.1.min.js:10(anonymous function) @ pouchdb-3.3.1.min.js:10(anonymous function) @ pouchdb-3.3.1.min.js:10(anonymous function) @ pouchdb-3.3.1.min.js:10(anonymous function) @ pouchdb-3.3.1.min.js:10$.ajax.success @ jav.js:352j @ jquery.js:3094k.fireWith @ jquery.js:3206x @ jquery.js:8259k.cors.a.crossDomain.send.b @ jquery.js:8600
这是我在 PouchDB 中偶尔遇到的错误。它发生在我:
- 获取所有任务
- 一个接一个,将每个任务发送到网络服务以存储在远程数据库中
- 返回响应后,从 PouchDB 中删除该任务 基本原理是,只有在网络服务确认后,它才会从手机上删除任务(使用此应用程序)。但问题是,由于这个错误,它永远不会删除第一个任务 - 这意味着许多重复项,因为用户会反复同步,想知道为什么它不工作。
- 继续下一个任务并从第 2 步开始重复。
错误发生在第 3 步附近,在 get 调用上。它似乎发生在第一次调用时(即,如果有 3 个任务,则尝试 get 第一个失败)。
我只是想强调一下——这种情况不会一直发生。只是每隔一段时间。通过谷歌搜索,我可以看到关于这个问题的文档并不多,但它似乎是由某处的竞争条件引起的。
我希望即使问题出在 PouchDB 本身,也许我可以通过某种方式重构我自己的代码,这样就不会造成太大问题。
var tasks = [];
myDatabase.allDocs({include_docs: true}).then(function(result) {
totalTasks = result.total_rows;
// Save the tasks
for (var i = 0; i < totalTasks; i++) {
tasks.push(result.rows[i].doc.task)
}
// If there are tasks...
if (tasks.length > 0) {
var syncLogic = function() {
// When the user clicks the sync button; send it off
var postData = {
// Use the username previously submitted
auth: {
username: username,
},
// Empty task because this is a shell - we'll overwrite that when we actually send the task
task: ''
};
// Link to the webservice
var postLink = syncHttpLink;
// Recursive function, because we need to send tasks one at a time
// That way if there's a failure, we'll never have to send the whole lot again
// and risk duplicate tasks.
var sendToWebService = function (count) {
postData.task = tasks[count];
count++;
var jsonStringifyPostData = JSON.stringify(postData);
$.ajax({
url: postLink,
cache: false,
type: 'POST',
timeout: 180000,
data: jsonStringifyPostData,
// When the data has been sent
complete: function () {
// Complete - nothing here currently
},
// When it's successful we'll get a response
success: function (data) {
if (!data.authenticates) {
// Log auth error
// ...
}
if (data.authenticates && data.synced) {
// They logged in, the task has synced. Delete the document from the internal database.
// THIS LINE HERE IS WHERE IT CAUSES THE ERROR:
myDatabase.get(data.id).then(function (doc) {
myDatabase.remove(doc, function(error, response) {
if (error) {
// Log the error
console.log("Database delete error:" + error);
}
else {
console.log("Database record deleted: " + data.id);
// Send the next task
if (count <= (totalTasks - 1)) {
sendToWebService(count);
}
else {
// Finished
exitSync();
}
}
});
});
}
},
// If there's an error...
error: function (xhr, status, error) {
console.log(error);
}
});
};
// First call!
sendToWebService(0);
};
}
});
感谢任何帮助。
【问题讨论】:
-
我打开了一个 Github 问题:github.com/pouchdb/pouchdb/issues/3802。在那里讨论可能比在 Stack Overflow 中讨论更实际。
-
还有一件事:如果您使用
Promise.all()而不是手动创建任务队列,您可以大大简化您的代码。您可以通过PouchDB.utils.Promise使用 PouchDB 中的 Promises。
标签: javascript jquery pouchdb