0
votes

I'm trying doing the following: I have a local database (using PouchDB), I check if user is logged in (with pouchdb-authentication login function) and if true I sync the locale db with the remote one.

Unfortunately, when I try to create a new database on CouchDB (I want one db for every user) I always get the error {"error":"not_found","reason":"no_db_file"}. I saw this is a common error described in PouchDB documentation (https://pouchdb.com/guides/databases.html#remote-databases) but CORS is enabled and I can't figure out where is the problem.

My couchdb configuration is: enter image description here

I do the login as follow:

var user = {
    name: 'name',
    password: 'password'
};
var url = "http://ip/";
var pouchOpts = {
    skipSetup: true
};
var ajaxOpts = {
    ajax: {
        headers: {
            Authorization: 'Basic ' + window.btoa(user.name + ':' + user.password)
        }
    }
};
var db = new PouchDB(url+'auth', pouchOpts);

db.login(user.name, user.password, ajaxOpts).then(function() {
        return db.allDocs();
}).then(function(docs) {

//HERE I TRY TO CREATE THE NEW DATABASE
pouchDBService.sync(url+"newDatabase", user);

}).catch(function(error) {
    console.error(error);
});

And, in my pouchDBService I have:

var database;

//I call this function as app starts
this.setDatabase = function(databaseName) {
   database = new PouchDB(databaseName, {
      adapter: 'websql'
   });
}

this.sync = function(remoteDatabase, user) {
    var remoteDB = new PouchDB(remoteDatabase, {
        auth: {
            username: user.name,
            password: user.password
        },
        skip_setup: true //without this I get the login popup! Why if I'm passing the auth params???
    });
    remoteDB.info().then(function (info) {
        console.log(info);
        database.sync(remoteDB, {live:true, retry: true})
    })
 }

Is there something wrong? Any help is appreciated.

Thanks

1

1 Answers

0
votes

To create databases on the CouchDB server, you need to be an admin. You could create a small custom API on the server for this (e.g. with a small node http server), or use the couchperuser plugin for CouchDB.