PouchDB db.query causes URL to have same index name as that of view name

94 views Asked by At

I am using PouchDB v3.6.0

I have a query like below:

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');
});

However, the above query is using the URL as below:

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

As can be seen in the Chrome debugger console, this causes a '404' error, as index name is same as view name.

However, if I manually correct the URL as below (which has correct index name) http://127.0.0.1:5984/ssarathi/_design/my_indexNew/_view/by_name1?include_docs=true&key=%22AADHIYA%22&_nonce=1434631865806

I can see the intended result.

Why db.query causes index name to have same as that of view name?

Thanks,

Sathya

2

There are 2 answers

2
nlawson On BEST ANSWER

db.query('foo') is shorthand for db.query('foo/foo'), where the part on the left is the design doc ID and the part on the right is the view name.

So since your design doc is _design/my_indexNew and your view is by_name1, you need to do this instead:

db.query('my_indexNew/by_name1')
0
sanar On

I used the below code to manipulate the result of query

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