Bill Noble
Bill Noble

Reputation: 6734

Connecting to two different databases with PouchDB

I am trying to connect and sync to two different databases with PouchDB in my App and CouchDB on the server. I can connect to one but the second connection doesn't work as it should.

My code looks like this:

    this._DB = new PouchDB('userdata');
let options = {
        live: true,   
        retry: true,  
        continuous: true
      };

this._syncHandler = this._DB.sync(remoteDB, options);

this._DB2 = new PouchDB('beer');
this._DB2.sync('localhost:5984/beer', options);
this._DB2.allDocs({})
        .then((doc)=> {
          console.log('****** TEST: doc = ' + JSON.stringify(doc));
        })
        .catch((err)=>{
          console.log('****** TEST: err = ' + JSON.stringify(err));
        });

When I run this code the console log lists the contents of the 'userdata' database and not the 'beer' database. This is odd and not what is intended.

Upvotes: 1

Views: 390

Answers (1)

smathy
smathy

Reputation: 27971

You've got a couple of things wrong here.

For a start, you have to include the protocol in the URL for the remote DB, ie. you need http:// in front of your localhost....

Then also, the sync call doesn't block, so you'll be calling allDocs before the actual replication has completed. If you try again a bit later you'll find that the docs will be there.

Upvotes: 1

Related Questions