首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PouchDB事务错误

PouchDB事务错误
EN

Stack Overflow用户
提问于 2015-05-06 07:11:36
回答 1查看 2.6K关注 0票数 2

我希望有人有一些想法。这给我带来了一些真正的问题:

代码语言:javascript
复制
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中遇到的错误。当我:

  1. 完成所有任务
  2. 一个接一个地将每个任务发送到webservice以存储在远程数据库中。
  3. 当响应被返回时,从PouchDB中删除该任务的基本原理是,只有当它被webservice确认后,它才会删除手机上的任务(这个应用程序就是在手机上使用的)。但是问题是,由于这个错误,它永远不会删除第一个任务--这意味着许多重复的任务,因为用户会反复同步,不知道为什么不工作。
  4. 继续执行下一个任务,并从步骤2开始重复。

该错误发生在步骤3的get调用中。它似乎发生在第一次通话时。如果有3个任务,则无法尝试get (第一个任务)。

我只想强调一下-这不是经常发生的。只是偶尔。通过谷歌搜索,我可以看到关于这个问题的文档不多,但似乎是由某个地方的种族状况引起的。

我希望即使问题是在PouchDB本身,也许有一些方法,我可以重构我自己的代码,这样就不会有太多的问题。

代码语言:javascript
复制
        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);
                };
            }
        });

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-06 12:11:41

嗯,我意识到了这个问题,但我认为只有在使用map/还原查询和destroy()时才有可能。如果您没有使用任何一个(而且看起来是这样的),那么这是一个非常有趣的数据点。你能提供一个活的测试用例来重现吗?

另外,您使用的是哪个浏览器以及PouchDB的哪个版本?

编辑:我用Promise.all重写了您的代码,这样您就可以看到它如何为您节省大量代码!与你的问题没有直接关系,但我喜欢教人们有关joy的承诺。:)

代码语言:javascript
复制
myDatabase.allDocs({
  include_docs: true
}).then(function(result) {
  // post all the data
  return PouchDB.utils.Promise.all(result.rows.map(function (row) {
    var postData = JSON.stringify({
      auth: { username: username },
      task: row.doc.task
    });
    return new PouchDB.utils.Promise(function (resolve, reject) {
        $.ajax({
          url: postLink,
          cache: false,
          type: 'POST',
          timeout: 180000,
          data: postData,

          success: function(data) {
            if (!data.authenticates) {
              reject(new Error('authentication error'));
            } else {
              // resolve the promise with the doc, so it will appear in the next step
              resolve(row.doc);
            }
          },

          error: reject
      }));
    });
  });
}).then(function (docs) {
  // remove all the docs
  return PouchDB.utils.Promise.all(docs.map(function (doc) {
    return myDatabase.remove(doc);
  }));
}).catch(function (err) {
  // any errors along the way will end up in a single place
  console.log(err);
});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30069878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档