离线启动应用程序时的数据库复制

Database replication when the application is launched offline

我使用PouchDB离线存储数据,应用上线时与远程CouchDB数据库同步。如果在线启动应用程序,它会很好地工作:PouchDB 在连接中断时触发 pause 事件,并在连接恢复时继续。这是一个示例代码:

localDB.replicate.to(remoteDB, {
  live: true,
  retry: true
}).on('paused', function (info) {
  // replication was paused, usually because of a lost connection
}).on('change', function (change) {
  // yo, something changed!
}).on('active', function (info) {
  // replication was resumed
}).on('error', function (err) {
  console.log(err);
  // totally unhandled error (shouldn't happen)
})

但是离线启动应用程序时遇到错误(Failed to load resource: net::ERR_ADDRESS_UNREACHABLE)

无法访问远程数据库,PouchDB 触发了一个 error 事件("Database encountered an unknown error",状态 500)。即使连接恢复,复制也不会起作用。

问题: 有没有一种方法可以使复制工作,即使应用程序是离线启动的(例如,即使还没有访问远程数据库,也可以使 pause 事件先出现)?

更新: 感谢 nlawson 的回答!我只需要在函数中添加远程数据库创建即可使其工作:

function retryReplication() {
  var remoteDB = new PouchDB('http://129.199.80.62:5984/remotedb');
  localDB.replicate.to(remoteDB, {
  live: true,
  }).on('paused', function (info) {
    // replication was paused, usually because of a lost connection
  }).on('change', function (change) {
    // yo, something changed!
  }).on('active', function (info) {
    // replication was resumed
  }).on('error', function (err) {
    setTimeout(retryReplication, 5000);
    // totally unhandled error (shouldn't happen)
  });
};

这应该是 Github 上的错误报告,而不是 Whosebug 上的问题。 :) 我filed it here.

retry 复制无论是在离线模式还是在线模式下都应该绝对有效。很抱歉你 运行 陷入这个错误!

与此同时,您可以避免 retry 选项并自己进行重试复制。你可以找到 a description of how to do that here.

更新:此错误已在 PouchDB 3.6.0 中修复。