我可以使用pouch-authentication插件从我的节点示例登录到couchDB到远程DB,但我不确定如何使用.sync方法进行复制,因为它在调用时不使用凭据。在pouch-authentication的文档中,它只是说:
但pouchdb-authentication API将在远程PouchDB对象上操作,而不是本地对象。
正因为如此,我一直得到一个401错误的未经认证/未经认证的凭据。
那么,我如何进行实时同步呢?下面是我的代码:
const PouchDB = require("pouchdb");
const PouchDBAuth = require('pouchdb-authentication');
PouchDB.plugin(PouchDBAuth)
// Create a new database instance
const localDB = new PouchDB('database')
const remoteDB = await new PouchDB("http://127.0.0.1/database", {skip_setup: true})
remoteDB.logIn('user', '12345678', function (err, response) {
if (err) {
if (err.name === 'unauthorized' || err.name === 'forbidden') {
console.log("4", err)
} else {
console.log("other")
}
}
remoteDB.getSession(function (err, response) {
if (err) {
// network error
} else if (!response.userCtx.name) {
// nobody's logged in
} else {
// response.userCtx.name is the current user
}
localDB.sync(remoteDB, {
live: true
}).on('change', function (change) {
// yo, something changed!
console.log("Yo, something changed")
}).on('error', function (err) {
console.log("Yo, we got an error", err)
// yo, we got an error! (maybe the user went offline?)
});
})
})
字符串
1条答案
按热度按时间sxpgvts31#
在
PouchDB Authentication
包的末尾概述有:Since version 1.0.0, this plugin does support Node.js.