electron 当pouchdb-authentication不支持.sync函数时,我如何将PouchDB与CouchDB同步?

0md85ypi  于 5个月前  发布在  Electron
关注(0)|答案(1)|浏览(57)

我可以使用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?)
                });
            })
       })

字符串

sxpgvts3

sxpgvts31#

PouchDB Authentication包的末尾概述有:Since version 1.0.0, this plugin does support Node.js.

相关问题