PouchDB交易错误

854

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

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. 一对一,将每个任务发送到Web服务以存储在远程数据库中
  3. 返回响应后,从PouchDB中删除该任务。基本原理是,只有在Web服务确认后,它才会从手机(使用此应用程序)中删除该任务。但是问题是,由于该错误,它永远不会删除第一个任务-意味着许多重复项,因为用户会反复同步以了解为什么它不起作用。
  4. 继续执行下一个任务,并从步骤2开始重复执行。

该错误发生在get呼叫的第3步附近它似乎是在第一次调用时发生的(即,如果有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);
                };
            }
        });

任何帮助表示赞赏。

劳森

嗯,我知道这个问题,但是我认为只有在使用map / reduce查询和时,才有可能destroy()如果您都不使用任何一个(似乎是这样),那么这是一个非常有趣的数据点。您可以提供一个现场测试用例进行复制吗?

另外,您使用的浏览器是什么版本的PouchDB?

编辑:我重写了您的代码,Promise.all所以您可以看到它如何为您节省大量代码!与您的问题没有直接关系,但是我喜欢向人们传授“诺言”的快乐。:)

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);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

实体框架交易错误

来自分类Dev

Firebase交易[错误:设置]

来自分类Dev

春天的交易错误

来自分类Dev

错误:找不到交易

来自分类Dev

错误:交易不活跃

来自分类Dev

PouchDB同步到错误的CouchDB

来自分类Dev

错误的交易状态(无活动交易,错误的交易类型或正在进行的交易)

来自分类Dev

MYSQL交易语法错误?

来自分类Dev

如何处理交易错误?

来自分类Dev

处理交易错误的最佳方法?

来自分类Dev

如何解决交易错误?

来自分类Dev

如何处理交易错误?

来自分类Dev

Vaadin和JPARepository:交易错误

来自分类Dev

交易错误向您付款

来自分类Dev

交易错误和 NameError python

来自分类Dev

发生错误,导致该交易被分类为不可提交的交易

来自分类Dev

如何在创建PouchDB时发现错误?

来自分类Dev

如何在创建PouchDB时发现错误?

来自分类Dev

使用 PouchDB 出现错误“文档更新冲突”

来自分类Dev

有交易时如何处理错误?

来自分类Dev

Neo4jClient交易错误

来自分类Dev

Postgres在将来的交易中返回错误

来自分类Dev

dnf崩溃了,现在有交易错误

来自分类Dev

pacman问题:“错误:准备交易失败”

来自分类Dev

SQLite的交易日期字段错误

来自分类Dev

Django:交易以及如何避免错误计数?

来自分类Dev

同步PouchDB和Couchbase同步网关-CORS错误

来自分类Dev

打开连接时PouchDB Chrome控制台错误

来自分类Dev

复制时出现pouchdb错误“未打开数据库”