سؤال

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);
        });
    }});
  }
});
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top