0
votes

I have a local PouchDB that must sync with a remote CouchDB. I'm trying (so far without success) to replicate the local one (on localhost:8081) to the remote one (on localhost:5984) through a POST call to /_replicate. No matter what I enter in the "source" field of my request, it appends "http:127.0.0.1:5984/" to the source url (or name) and, obviously, I get an error saying it can't find the source db. Of course.

My question (which is, by the way, my very first question on StackOverFlow so please be indulgent) is then: how can I point to the correct db? Thanks!

let url = `http://127.0.0.1:5984/_replicate`;
let data = {
    // This is where I struggle
    "source" : "http://127.0.0.1:8081/_pouch_local_db",
    "target" : `http://127.0.0.1:5984/mydb-${username}`
}
fetch(url, {
    method: 'POST',
    body: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json',
    },
    credentials: 'include',
  }).then(response => {
    console.log('Success syncing: ', response);
  }).catch(error => console.error('Error while syncing: ', error));
1

1 Answers

0
votes

Your code suggests you are asking CouchDb to replicate with a local PouchDb database? That will not work as PouchDb is intended to be a client-side database system. In normal use, PouchDb is not a server (I know there is a PouchDb server available but I do not think that is of help here).

You should use the PouchDb API (include the PouchDB library in your Javascript code) and tell your local PouchDb database to replicate with the CouchDb server on port 5984.

I would expect your code to look like this:

var dblocal = new PouchDB('local_db');
var dbremote = new PouchDB('http://127.0.0.1:5984/myremotedb');
dblocal.replicate.to(dbremote, function (err, result) {
  if (err) { return console.log(err); }
  // handle 'completed' result
});

I am assuming you have not password-protected your test CouchDb database. If you have, you will need to include a username and password in the "auth" options for the remote database when using new PouchDB.

There are some examples and information on the PouchDB site.