不能用pouchdb保存docindex。

问题描述 投票:0回答:1

我试图在我的数据上创建一个索引,但我一直得到这个错误。 我已经临时删除了数据库的所有权限以使其工作,但仍然没有成功。

{
  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个回答
0
投票

我相信pouchdb-authentication是一个复杂的问题。[1],有已知的问题,关于该插件[2,3].

我是 意见 在HTTPS上使用Basic Authentication是大多数使用情况下的最佳选择,我认为pouchdb-authentication在需要经常切换凭证来执行各种任务的情况下非常方便。我认为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安全 2pouchdb-authentication issue #243 3pouchdb-authentication问题 #204 4添加CORS到CouchDB
© www.soinside.com 2019 - 2024. All rights reserved.