user3676101
user3676101

Reputation: 17

PouchDB to CouchDB replication Ionic app

Im writting Ionic app. Im using PouchDB as local storage and I want to replicate these data to remote CouchDB server iriscouch.com, but I can`t make it work. When I try to replicate db I get error shown below. If I start server localy on my laptop it works (without credentials - admin party).

var db = new PouchDB('db');

db.replicate.to('https://username:[email protected]:5984/db', {xhrFields:{withCredentials:true}})
        //.on('change', function (info) {
            // handle change
          //  console.log("change");
        //    console.log(info);
        .on('uptodate', function (info) {
            // handle up-to-date
            console.log("uptodate");
            console.log(info);
        }).on('error', function (err) {
            // handle error
            console.log("error");
            console.log(err);
        });

OPTIONS https://username.iriscouch.com:5984/db/?_nonce=VFSibfdoxcnjKz3V net::ERR_CONNECTION_CLOSED pouchdb.js:5150

CustomPouchError {message: undefined, status: 405, statusText: "Method Not Allowed", name: "unknown_error", error: true…} services.js:24

Settings of my CouchDB

curl -X PUT $HOST/_config/httpd/enable_cors -d '"true"'
curl -X PUT $HOST/_config/cors/origins -d '"*"'
curl -X PUT $HOST/_config/cors/credentials -d '"true"'
curl -X PUT $HOST/_config/cors/methods -d '"GET, PUT, POST, HEAD, DELETE"'
curl -X PUT $HOST/_config/cors/headers -d '"accept, content-type, origin, referer"'

To make it work:

var remoteUrl = 'https://username.iriscouch.com/dbname' 
var remote = new PouchDB(remoteUrl, {
Auth: {
    username: 'username',
    password: 'password'
},
live:true,
withCredentials: true

});

Upvotes: 0

Views: 1472

Answers (2)

Moghammed
Moghammed

Reputation: 11

I had the same problem and was able to fix it by adding "x-csrf-token" to the headers config in the cors section of the iriscouch configuration.

That makes my headers config:

headers: accept, authorization, referer, origin, x-csrf-token, content-type

I hope this helps anyone else.

Upvotes: 1

nlawson
nlawson

Reputation: 11620

405 is a CORS error; that means you still don't have CORS set up correctly. Did you try this? https://github.com/pouchdb/add-cors-to-couchdb

Upvotes: 3

Related Questions