Google App Engine 上的 CouchDB 登录访问
CouchDB login access on Google App Engine
我是 运行 Google App Engine 上的 CouchDB 实例。我已经成功 . I have enabled CORS and am using https. I have added NETWORK: *
to my cache manifest.
现在我需要将我的 PouchDB 数据同步到我的远程 CouchDB 数据库。
这个相关问题 Couchdb sync access with userid and password 展示了如何在 cloudant 上托管的 CouchDB 实例上传递用户 ID 和密码。 (我还检查了所有其他相关问题。)
是否有类似的方法可以在 Google App Engine 上执行此操作?
是这样的吗?
https://<api_key>:<key_passwd>@<username>.<my_ip_address>:5984/<db_name>
根据 PouchDB: getting started 格式为 http://user:pass@myname.example.com/dbname
不过我不确定是应该使用主机名 (myapp.appspot.com
) 还是主机的 IP 地址。
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));
});
控制台中的错误消息
Uncaught TypeError: this.remote.login is not a function
感谢ptitjes
on Github for an answer on what was going wrong with the login()
function:
you are trying to call login()
on a string (your this.remote
is a
string). Hence the TypeError: this.remote.login is not a function
. You
should have done: this.remote = new PouchDB(remoteorigin + '/myPdb');
(我发现这很混乱,因为远程数据库是 CouchDB,但你知道了。)
登录正常,但同步仍然无效。
登录功能
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 = 'pouchnotes';
var remoteorigin = 'https://ip-address:5984';
this.dbname = databasename;
this.pdb = new PouchDB(databasename);
this.remote = new PouchDB(remoteorigin + '/pouchnotes');
var user = {
name: 'admin',
password: 'password'
};
var pouchOpts = {
skipSetup: true
};
var ajaxOpts = {
ajax: {
headers: {
Authorization: 'Basic ' + window.btoa(user.name + ':' + user.password)
}
}
};
this.remote.login(user.name, user.password, ajaxOpts, function (err, response) {
if (err) {
if (err.name === 'unauthorized' || err.name === 'forbidden') {
console.log('Unauthorised user');
} else {
//return this.remote.allDocs();
console.log('Successful login');
}
}
});
var opts = {live: true};
this.pdb.replicate.to(this.remote, opts);
this.pdb.replicate.from(this.remote, opts);
};
同步功能
我是 运行 Google App Engine 上的 CouchDB 实例。我已经成功 NETWORK: *
to my cache manifest.
现在我需要将我的 PouchDB 数据同步到我的远程 CouchDB 数据库。
这个相关问题 Couchdb sync access with userid and password 展示了如何在 cloudant 上托管的 CouchDB 实例上传递用户 ID 和密码。 (我还检查了所有其他相关问题。)
是否有类似的方法可以在 Google App Engine 上执行此操作?
是这样的吗?
https://<api_key>:<key_passwd>@<username>.<my_ip_address>:5984/<db_name>
根据 PouchDB: getting started 格式为 http://user:pass@myname.example.com/dbname
不过我不确定是应该使用主机名 (myapp.appspot.com
) 还是主机的 IP 地址。
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));
});
控制台中的错误消息
Uncaught TypeError: this.remote.login is not a function
感谢ptitjes
on Github for an answer on what was going wrong with the login()
function:
you are trying to call
login()
on a string (yourthis.remote
is a string). Hence theTypeError: this.remote.login is not a function
. You should have done:this.remote = new PouchDB(remoteorigin + '/myPdb');
(我发现这很混乱,因为远程数据库是 CouchDB,但你知道了。)
登录正常,但同步仍然无效。
登录功能
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 = 'pouchnotes';
var remoteorigin = 'https://ip-address:5984';
this.dbname = databasename;
this.pdb = new PouchDB(databasename);
this.remote = new PouchDB(remoteorigin + '/pouchnotes');
var user = {
name: 'admin',
password: 'password'
};
var pouchOpts = {
skipSetup: true
};
var ajaxOpts = {
ajax: {
headers: {
Authorization: 'Basic ' + window.btoa(user.name + ':' + user.password)
}
}
};
this.remote.login(user.name, user.password, ajaxOpts, function (err, response) {
if (err) {
if (err.name === 'unauthorized' || err.name === 'forbidden') {
console.log('Unauthorised user');
} else {
//return this.remote.allDocs();
console.log('Successful login');
}
}
});
var opts = {live: true};
this.pdb.replicate.to(this.remote, opts);
this.pdb.replicate.from(this.remote, opts);
};
同步功能