PouchDB db.query 导致 URL 具有与视图名称相同的索引名称
PouchDB db.query causes URL to have same index name as that of view name
我正在使用 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,如下所示:
正如在 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导致索引名称与视图名称相同?
谢谢,
沙迪亚
db.query('foo')
是shorthand for db.query('foo/foo')
,其中左边部分是设计文档ID,右边部分是视图名称。
因此,由于您的设计文档是 _design/my_indexNew
,而您的视图是 by_name1
,因此您需要这样做:
db.query('my_indexNew/by_name1')
我使用下面的代码来操作查询结果
db.query('idx/bypin', {
key: myId,
include_docs: true
}).then(function(result) {
// do something with result object }
...
}
我正在使用 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,如下所示:
正如在 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导致索引名称与视图名称相同?
谢谢,
沙迪亚
db.query('foo')
是shorthand for db.query('foo/foo')
,其中左边部分是设计文档ID,右边部分是视图名称。
因此,由于您的设计文档是 _design/my_indexNew
,而您的视图是 by_name1
,因此您需要这样做:
db.query('my_indexNew/by_name1')
我使用下面的代码来操作查询结果
db.query('idx/bypin', {
key: myId,
include_docs: true
}).then(function(result) {
// do something with result object }
...
}