How to replicate from CouchDB to PouchDB?
Asked Answered
P

1

7

I've set up a local CouchDB database and I'd like to replicate it to a PouchDB database, using JavaScript in a web page running on localhost.

With the code below I get this error:

Origin http://localhost is not allowed by Access-Control-Allow-Origin.

With http:// removed from REMOTE, I don't get an error, but no docs are shown as replicated.

Looking at IndexedDB databases from Chrome DevTools, I can see the database has been created (but doesn't appear to have documents).

Running in Chrome 29.0.1535.2 canary.

Can I do this locally, or do I need to set up a remote CouchDB database and enable CORS (as per the CouchDB docs)?

var REMOTE = 'http://127.0.0.1:5984/foo';
var LOCAL = 'idb://foo';

Pouch(LOCAL, function(error, pouchdb){
  if (error) {
    console.log("Error: ", error);
  } else {
    var db = pouchdb;
    Pouch.replicate(REMOTE, LOCAL, function (error, changes) {
      if (error) {
        console.log('Error: ', error);
      }
      else {
        console.log('Changes: ', changes);
        db.allDocs({include_docs: true}, function(error, docs) {
          console.log('Rows: ', docs.rows);
        });
    }});
  }
});
Polished answered 11/6, 2013 at 15:31 Comment(0)
U
10

You can do it locally, but CORS has to be enabled.

When you remove "http://" from the remote URL, Pouch is going to replicate your DB into a new IndexedDB-backed Pouchdb named "localhost" (or actually "_pouch_localhost" or something like that - it adds a prefix).

Unless you're serving up this page from CouchDB itself (on the same host & port), you will need to enable CORS to get replication to CouchDB working.

Uncanny answered 12/6, 2013 at 15:47 Comment(5)
Can I enable CORS for a CouchDB server on localhost? Tried enabling CORS from Futon (httpd > cors to true, and cors > credentials both true and false) but still getting errors: OPTIONS http://127.0.0.1:5984/presentations/ 405 (Method Not Allowed) pouchdb-nightly.min.js:1 OPTIONS http://127.0.0.1:5984/presentations/ Origin http://localhost is not allowed by Access-Control-Allow-Origin. pouchdb-nightly.min.js:1 XMLHttpRequest cannot load http://127.0.0.1:5984/foo/. Origin http://localhost is not allowed by Access-Control-Allow-Origin. localhost/node/foo/:1 ... Object {status: 0}Polished
It shouldn't be any different on localhost. Maybe try restarting Couch after setting httpd > enable_cors = true in the config?Uncanny
Thanks @chesles -- good suggestion, but tried restarting CouchDB (and the machine it's running on!) but still get the cross origin error: XMLHttpRequest cannot load http://127.0.0.1:5984/foo/. Origin http://localhost is not allowed by Access-Control-Allow-Origin. I've checked in Futon on restart: httpd > enable_cors is truePolished
@SamDutton Do you have anything under the cors > origins config section? If so, make sure the list includes http://localhost. You may also want to do a sanity check and make sure you're running Couch 1.3.x, as 1.2 and lower didn't have CORS support built-in.Uncanny
BINGO: adding cors > origins did the trick. Thanks for your tenacity!Polished

© 2022 - 2024 — McMap. All rights reserved.