PouchDb Replication Failed for CouchDb

0

Scenario

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 only occurring in one direction (Server => Local), and I do not get any error.

When analyzing traffic on the Network tab, I see that ${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 replication request.

Auditing Replication

When auditing the event complete and error , I do not catch any errors and I receive, and the complete does not report any irregularities.

['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"
  }
}

Manual Replication

Now when trying to manually replicate from Server to 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 do not get anything in complete , but I get a erro , but it does not even 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 Errors in 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 log has been updated, and follows the return.:

{
  "ok": true,
  "id": "4b0ea507-cd88-4998-baf0-01629b50516b",
  "rev": "2-d9363f28e53fdc145610f5ad3f75a043"
}
    
asked by anonymous 06.04.2018 / 17:10

1 answer

1

Source of Response .: Webpack: Promise is not a constructor

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

Since PouchDb is using polyfill for Promise , even when the environment supports ES2016 .

So every time he made the following assignment.:

var Promise = require('pouchdb-promise')

he was doing 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

An unimportant way of solving this problem, series make a replace on all packages on node_modules .

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

However this would make the process of updating the packages unfeasible, as this error would return every time the branch was downloaded.

If we look at the folder node_modules\pouchdb-promise\lib , we can see two files, a index.es.js and index.js . which is the index.es.js that is processed when the pouchdb-promise package is required.

So as a contour solution, just add an alias in the configuration from webpack to pouchdb-promise , so it reads index.js instead of index.es.js .

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

As I'm using Quasar Framework , I made this change in the file quasar.config.js

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