I am running a CouchDB instance on Google App Engine. I have successfully created a SSH tunnel to access Fauxton on it via localhost. I have enabled CORS and am using https. I have added NETWORK: *
to my cache manifest.
Now I need to sync my PouchDB data to my remote CouchDB database.
This related question Couchdb sync access with userid and password shows how to pass in the userid and password on a CouchDB instance hosted on cloudant. (I've also checked all the other related questions.)
Is there a similar method to use for doing this on Google App Engine?
Something like this?
https://<api_key>:<key_passwd>@<username>.<my_ip_address>:5984/<db_name>
According to PouchDB: getting started the format is http://user:pass@myname.example.com/dbname
I'm not sure if I should use the hostname (myapp.appspot.com
) or the host's IP address though.
Code in application.js
PouchNotesObj = function (databasename, remoteorigin) {
'use strict';
Object.defineProperty(this, 'pdb', {writable: true});
Object.defineProperty(this, 'remote', {writable: true});
Object.defineProperty(this, 'formobject', {writable: true});
Object.defineProperty(this, 'notetable', {writable: true});
Object.defineProperty(this, 'searchformobject', {writable: true});
Object.defineProperty(this, 'errordialog', {writable: true});
Object.defineProperty(this, 'dbname', {writable: true});
var databasename = 'myPdb';
var remoteorigin = 'https://<IP ADDRESS>';
this.dbname = databasename;
this.pdb = new PouchDB(databasename);
this.remote = remoteorigin + '/myPdb';
//from https://github.com/pouchdb-community/pouchdb-authentication/issues/121
var user = {
name: 'admin',
password: '<myPassword>'
};
var pouchOpts = {
skipSetup: true
};
var ajaxOpts = {
ajax: {
headers: {
Authorization: 'Basic ' + window.btoa(user.name + ':' + user.password)
}
}
};
this.remote.login(user.name, user.password, ajaxOpts).then(function() {
return db.allDocs();
}).then(function(docs) {
console.log(docs);
}).catch(function(error) {
console.error(error);
});
};
pouchdb.authentication.js
exports.login = utils.toPromise(function (username, password, opts, callback) {
var db = this;
if (typeof callback === 'undefined') {
callback = opts;
opts = {};
}
if (['http', 'https'].indexOf(db.type()) === -1) {
return callback(new AuthError('this plugin only works for the http/https adapter'));
}
if (!username) {
return callback(new AuthError('you must provide a username'));
} else if (!password) {
return callback(new AuthError('you must provide a password'));
}
var ajaxOpts = utils.extend(true, {
method : 'POST',
url : utils.getSessionUrl(db),
headers : {'Content-Type': 'application/json'},
body : {name: username, password: password}
}, opts.ajax || {});
utils.ajax(ajaxOpts, wrapError(callback));
});
Error message in Console
Uncaught TypeError: this.remote.login is not a function
Thanks to
ptitjes
on Github for an answer on what was going wrong with thelogin()
function:(I find that confusing because the remote database is CouchDB, but there you go.)
The login is now working but the sync still isn't working.
Login function
Sync function
See: Manav Manocha's version of PouchNotes on Github