Failed Pouchdb Replication for Couchdb

Asked

Viewed 51 times

0

Caenarius

I have the following code.:

['tableA', 'tableB', 'tableC'].forEach(name => {
    let local = new PouchDB(name, { auto_compaction: true })
    let server = new PouchDB(serverUrl + name)

    var filtro = {
      include_docs: true,
      filter: 'replication/by_dispositivo',
      query_params: { 'dispositivo_id': obj.deviceId }
    }
    local.replicate.from(server, filtro).on('complete', report => {
      var sync = local.sync(server, {
        live: true,
        retry: true,
        ...filtro
      })
    })
})

However, replication is occurring successfully only in one direction (Server => Local), and I get no error.

When analyzing traffic in the tab Network, I see that the ${serverUrl}/{name}/_revs_diff is bringing the expected result.:

{"4b0ea507-cd88-4998-baf0-01629b50516b":{"missing":["2-2133d30de8d44ebd958cee2b68726ffb"],"possible_ancestors":["1-39904a7e55b1cb266c840a2acf34fdc2"]}}

that is, there is a local pedente replication review.

Auditando Replicação

when auditing the event complete and error, do not catch any error and receive, and complete does not report any irregularity.

['tableA', 'tableB', 'tableC'].forEach(name => {
    let local = new PouchDB(name, { auto_compaction: true })
    let server = new PouchDB(serverUrl + name)

    let filtro = {
      include_docs: true,
      filter: 'replication/by_dispositivo',
      query_params: { 'dispositivo_id': obj.deviceId }
    }
    local.replicate.from(server, filtro).on('complete', report => {
      let sync = local.sync(server, {
        live: true,
        retry: true,
        ...filtro
      })
      sync.on('error', (error) => {
        console.error(error)
        console.error(JSON.stringify(error, null, 2))
      }).on('complete', (result) => {
        console.log(result)
        console.log(JSON.stringify(result, null, 2))
      })
      window.setTimeout(function (evt) {
        state.syncProcess[database].cancel()
      }, 15000)
    })
})

Result.json

{
  "push": {
    "ok": true,
    "start_time": "2018-04-06T15:00:42.266Z",
    "docs_read": 0,
    "docs_written": 0,
    "doc_write_failures": 0,
    "errors": [],
    "status": "cancelled",
    "end_time": "2018-04-06T15:00:42.266Z",
    "last_seq": 0
  },
  "pull": {
    "ok": true,
    "start_time": "2018-04-06T15:00:26.422Z",
    "docs_read": 0,
    "docs_written": 0,
    "doc_write_failures": 0,
    "errors": [],
    "last_seq": "17-g1AAAAJDeJyd0EsOgjAQBuAqJj52nkCPILGldCU3UaYzBA3CQl3rTfQmehO9CRZKAiaGiJtpMs18mX8SxtgodpBNdXbSMUKQZDpM4uxwTMxXP2Qwy_N8Fzsh25vGMILIA62QjU8pUrRNCVvGYW4qrCphUgoCfMVd_W2mTQoKaV1JjpWWIUcuu0qbQjp_pBKeUESLH1OlA1PZxTwGudb7EC1dQt5xH6vdrHYvtF6pSZK-4Oov7WG1Z53QUy56UnRK-LJK406-TxIAm8ruDdzts44",
    "status": "cancelled",
    "end_time": "2018-04-06T15:00:41.427Z"
  }
}

Replicação Manual

Now trying to manually replicate Server for Local

['tableA', 'tableB', 'tableC'].forEach(name => {
    let local = new PouchDB(name, { auto_compaction: true })
    let server = new PouchDB(serverUrl + name)

    let filtro = {
      include_docs: true,
      filter: 'replication/by_dispositivo',
      query_params: { 'dispositivo_id': obj.deviceId }
    }
    local.replicate.from(server, filtro).on('complete', report => {
      local.replicate.to(server, filtro).on('complete', report => {
        console.log(report)
        console.log(JSON.stringify(report, null, 2))
        let sync = local.sync(server, {
          live: true,
          retry: true,
          ...filtro
        })
      }).on('error', (error) => {
        console.error(error)
        console.error(JSON.stringify(error, null, 2))
      })
    })
})

I get nothing on complete, but I capture a erro, but it doesn’t help me at all.:

Error.json

{
  "result": {
    "ok": false,
    "start_time": "2018-04-06T15:07:19.105Z",
    "docs_read": 1,
    "docs_written": 0,
    "doc_write_failures": 0,
    "errors": [],
    "status": "aborting",
    "end_time": "2018-04-06T15:07:19.768Z",
    "last_seq": 3
  }
}

Sending Changes Manually - Checking for No Errors in the Authorization Script

var deviceId = ''
var listaLocal = []
var listaServer = []

getDeviceId().then(response => {
    deviceId = response
    return local.find({ selector: { dispositivo_id: deviceId } })
}).then(response => {
    listaLocal = response.docs
    return server.find({ selector: { dispositivo_id: deviceId } })
}).then(response => {
    listaServer = response.docs

    var tlocal = listaLocal[0]
    var tServer = listaServer[0]

    Object.keys(tlocal).forEach(key => {
      if (key.indexOf("_") !== 0) {
        tServer[key] = tlocal[key]
      }
    })

    return server.talao_para_aceites.put(tServer).then(result => {
      console.log(result)
      console.log(JSON.stringify(result, null, 2))
    }).catch(error => {
      console.error(error)
      console.error(JSON.stringify(error, null, 2))
    })
})

The remote record has been updated, and follows the return.:

{
  "ok": true,
  "id": "4b0ea507-cd88-4998-baf0-01629b50516b",
  "rev": "2-d9363f28e53fdc145610f5ad3f75a043"
}

1 answer

1


Source of the Answer.: Webpack: Promise is not a constructor

The problem was a misunderstanding between the pouchdb-promise and the webpack.

Like the PouchDb is using a polyfill to the Promise, even when the environment supports the ES2016.

So every time he did the following assignment.:

var Promise = require('pouchdb-promise')

he was making the following assignment.:

var lie = require('lie')
var Promise = { 'default': typeof Promise === 'function' ? Promise : lie }

instead of performing the following assignment.:

var lie = require('lie')
var Promise = typeof Promise === 'function' ? Promise : lie

Not an elegant way to solve this problem replace in all the packages in node_modules.

Localizar: var Promise = require('pouchdb-promise')
Substituir: var Promise = require('pouchdb-promise').default

However, this would make the process of upgrading the packages unviable, just as this error would return every time the branch was downloaded.

If we look at the briefcase node_modules\pouchdb-promise\lib, we can notice two files, one index.es.js and index.js. being that is the index.es.js which is processed when the package pouchdb-promise is required.

Then as an outline solution, simply add an alias to the webpack to the pouchdb-promise, so that it reads the index.js instead index.es.js.

resolve: {
  alias: {
    'pouchdb-promise$': "pouchdb-promise/lib/index.js"
  }
}

how I’m using the Quasar Framework, I made this change in the archive quasar.config.js

build: {
  ...
  extendWebpack (cfg) {     
    ...   
    cfg.resolve.alias['pouchdb-promise$'] = 'pouchdb-promise/lib/index.js'    
    ... 
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.