【问题标题】:Cannot save ddoc/index with pouchdb无法使用 pouchdb 保存 ddoc/index
【发布时间】:2020-09-01 19:06:58
【问题描述】:

我正在尝试为我的数据创建索引,但我不断收到此错误。我已经暂时从数据库中删除了所有权限以使其正常工作,但仍然没有成功。

{
  error: 'error_saving_ddoc',
  reason: 'Unknown error while saving the design document: unauthorized',
  ref: 612684199,
  status: 500,
  name: 'error_saving_ddoc',
  message: 'Unknown error while saving the design document: unauthorized'
}

我的代码:

(async () => {
    let db:any = new PouchDB('http://localhost:5984/test', { skip_setup: true });
    await db.createIndex({
      index: {fields: ['area']}
    })
    let res = await db.find({
        selector: {'area': {$gt: null}},
        fields: ['_id', 'area'],
        sort: ['_id']
      });
})();

我也尝试安装pouchdb-authentication并使用下面的代码成功登录,但我仍然无法使用上面的代码创建索引。

授权码:

this.db.logIn('admin', 'password').then((x)=>{
  console.log("This works");
}).catch(e=>console.log(e));

我应该怎么做才能让它工作?

【问题讨论】:

    标签: couchdb pouchdb couchdb-futon


    【解决方案1】:

    我认为 pouchdb-authentication 很复杂[1],并且关于该插件存在已知问题[2,3]

    我是意见 使用基于 HTTPS 的基本身份验证最适合大多数用例。我发现 pouchdb-authentication 在需要经常切换凭据以执行各种任务的情况下非常方便。

    以下代码演示了不同的身份验证方式。此代码适用于 nodejs,但很容易适应浏览器,前提是服务器端 CORS 设置正确[4]

    let PouchDB = require("pouchdb");
    PouchDB.plugin(require("pouchdb-find"));
    PouchDB.plugin(require("pouchdb-authentication"));
    
    const testUrlTemplate = "http://*127.0.0.1:5984/test";
    
    const doTask = async (db) => {
      await db.createIndex({
        index: { fields: ["area"] },
      });
    
      return await db.find({
        selector: { area: { $gt: null } },
        fields: ["_id", "area"],
        sort: ["area"],
      });
    };
    
    // connect to remote db using basic auth (preferred with https)
    const basicAuth = async (credentials) => {
      const url = testUrlTemplate.replace("*", "");
      return new PouchDB(url, {
        skip_setup: true,
        auth: credentials,
      });
    };
    // connect to remote db using unwise cleartext 
    const unwiseUrl = async (credentials) => {
      const url = testUrlTemplate.replace(
        "*",
        `${credentials.username}:${credentials.password}@`
      );
      return new PouchDB(url, {
        skip_setup: true,
      });
    };
    
    // convenience.
    const log = (obj) => {
      console.log(JSON.stringify(obj));
    };
    
    (async () => {
      try {
        const credentials = {
          username: "some_user",
          password: "some_password",
        };
    
        let db = await basicAuth(credentials);
        let result = await doTask(db);
        log(result);
    
        db = await unwiseUrl(credentials);
        result = await doTask(db);
        log(result);
    
        // use basic auth the init the db, then switch to another user
        // with a cookie session.
        db = await basicAuth(credentials);
        await db.logIn("plain_user", "plaintext_password");
        result = await doTask(db);
        log(result);
      } catch (err) {
        // oopsy
        log(result);
      }
    })();
    

    请注意我更改了您的查找代码,因为 sort 参数会破坏查询。


    1PouchDB Security
    2pouchdb-authentication issue #243
    3pouchdb-authentication issue #204
    4Add CORS to CouchDB

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2020-01-02
      • 1970-01-01
      相关资源
      最近更新 更多