【问题标题】:PouchDB db.query causes URL to have same index name as that of view namePouchDB db.query 导致 URL 具有与视图名称相同的索引名称
【发布时间】:2015-06-19 19:01:37
【问题描述】:

我正在使用 PouchDB v3.6.0

我有一个如下查询:

var ddoc = {
  _id: '_design/my_indexNew',
  views: {
    by_name1: {
      map: function (doc) {  emit(doc.Name); }.toString()
    }
  }
};
// save it
db.put(ddoc).then(function () {
    alert('Idx created');
  // success!
}).catch(function (err) {
    if (err.status != 409) { // some error other than  409 (already exists)
    alert(err);
    }

});

 // Now use the Query

    var myId = 'AADHIYA';

db.query('by_name1', {
  key          : myId, 
  include_docs : true
}).then(function (result) {
  // handle result
    alert('found');
}).catch(function (err) {
  // handle errors
    alert('not found');
});

但是,上面的查询使用的 URL 如下:

http://127.0.0.1:5984/ssarathi/_design/by_name1/_view/by_name1?include_docs=true&key=%22AADHIYA%22&_nonce=1434631865806

从 Chrome 调试器控制台中可以看出,这会导致“404”错误,因为索引名称与视图名称相同。

但是,如果我手动更正 URL 如下(具有正确的索引名称) http://127.0.0.1:5984/ssarathi/_design/my_indexNew/_view/by_name1?include_docs=true&key=%22AADHIYA%22&_nonce=1434631865806

我可以看到预期的结果。

为什么 db.query 导致索引名称与视图名称相同?

谢谢,

沙迪亚

【问题讨论】:

    标签: pouchdb


    【解决方案1】:

    db.query('foo')db.query('foo/foo') 的简写,其中左侧部分是设计文档 ID,右侧部分是视图名称。

    因此,由于您的设计文档是 _design/my_indexNew 而您的视图是 by_name1,因此您需要改为:

    db.query('my_indexNew/by_name1')
    

    【讨论】:

    • 谢谢.. 成功了!从上面的查询中,我可以存储结果并输出吗?
    • 它已经被索引了,所以不需要再次存储它。只需在需要时查询它。 :)
    【解决方案2】:

    我使用下面的代码来操作查询的结果

    db.query('idx/bypin', {
         key: myId,
         include_docs: true
         }).then(function(result) {
             // do something with result object }
         ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      • 2017-03-22
      相关资源
      最近更新 更多