【问题标题】:pouchdb-adapter-cordova-sqlite works locally but is not syncingpouchdb-adapter-cordova-sqlite 在本地工作但不同步
【发布时间】:2019-06-20 19:51:02
【问题描述】:

我在 PouchDB 中使用 cordova-sqlite 适配器时遇到问题。它在本地完美运行(我可以创建、更新和删除文档),但无法与任何远程 CouchDB 服务器同步。我尝试了 IBM Cloudant 和我自己的 CouchDB 服务器。

如果我使用 IDB 适配器,它就像一个魅力,但当我更改为 SQLite 时,它​​无法正确同步。

这就是我使用它的方式(在我的情况下是在 Vue 应用程序中):

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import PouchDB from 'pouchdb'
...
Object.defineProperty(Vue.prototype, '$pouch', { value: PouchDB }); //this is to be able to use $pouch in any Vue component later on
...
var PouchAdapterCordovaSqlite = require('pouchdb-adapter-cordova-sqlite');
PouchAdapterCordovaSqlite.use_prefix = true; // use the legacy '_pouch' prefix. I tried both with and without this option
PouchDB.plugin(PouchAdapterCordovaSqlite); 
...        
this.db = new this.$pouch('todos', { adapter: 'cordova-sqlite', location: 'default', androidDatabaseImplementation: 2 });

console.log('PouchDB adapter: ' + this.db.adapter); //it returns 'cordova-sqlite'

let remoteCouch='http://myserveraddress.com:5984/todos'; //todos is the remote database, with CORS enabled and set as public so no user/pass is needed

this.$pouch.sync('todos', remoteCouch, {
        live: true,
        retry: true
      }).on('change', function (info) { console.log('change:' + info}).on('paused', function (err) {//...//}).on --- and so on with 'active', 'denied', 'complete' and 'error'

后来在一个 Vue 组件中我使用它(其中 this.db 指的是数据库,而 this.todos 用于在屏幕上显示结果:

    mounted() {
        this.list();
        this.db
          .changes({
            since: "now",
            live: true
          })
          .on("change", this.list);
      },
      methods: {
      list: function() {
      let self = this;
      this.db.allDocs({ include_docs: true, descending: true }, function(
        err,
        doc
      ) {
        self.todos = doc.rows;
      });
    }
}

正如我之前提到的,它适用于 IndexedDB,但不适用于 SQLite 适配器(我同时使用了 cordova-sqlite-storage 和 cordova-plugin-sqlite-2,结果相同)。一切都在 deviceready 事件和 sqlite 正确加载后开始(我可以使用 window.sqlitePlugin)。

当涉及到 Cordova config.xml 时,我确保定义了这个:

<plugin name="cordova-plugin-whitelist"/>
<access origin="*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
...
<plugin name="cordova-plugin-sqlite-2" spec="*"/>

有什么线索吗?我真的需要使用 SQLite 而不是 IDB,因为我的应用会存储大量数据,而且我还需要 offline-first 方法,这就是我使用 PouchDB 的原因。

我尝试的另一个测试是从我的应用程序手动创建一个 SQLite 数据库,它也可以工作,所以问题似乎与 pouchdb-adapter-cordova-sqlite 有关。

IDB 的屏幕截图: result with idb

以及使用 SQLite 的屏幕截图: result with sqlite

提前致谢!

【问题讨论】:

    标签: sqlite cordova vue.js couchdb pouchdb


    【解决方案1】:

    解决了!

    问题出在这里:

    this.$pouch.sync('todos', remoteCouch, {...
    

    必须改为:

    this.db.sync(remoteCouch, {...
    

    所以它指向 pouchdb 对象而不是数据库实例。 我希望它可以帮助某人。 问候!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-19
      • 2014-12-19
      • 2020-11-19
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      相关资源
      最近更新 更多