У меня возникли проблемы с созданием индекса для поиска в pouchDB с использованием pouchdb-find. Я продолжаю получать ошибку couldn't find a usable index. try creating an index on: dateCreated. Make sure that only $eq, $gt, $gte, $lt, and $lte are used for the indexed fields.
- но я не совсем уверен, что это значит или как ее исправить.Создание полезного индекса в PouchDB с помощью pouchdb-find
Вот мой код:
PouchDB.debug.enable('pouchdb:find');
var testDB = new PouchDB('testDB');
testDB.destroy('testDB').then(function() {
testDB = new PouchDB('testDB');
}).then(function() {
return testDB.bulkDocs([
{siteTitle : 'Google Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'google.com', _id: 'doc1'},
{siteTitle : 'Google Mail', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'google.com', _id: 'doc2'},
{siteTitle : 'Yahoo Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'yahoo.com', _id: 'doc3'},
{siteTitle : 'Bing Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'bing.com', _id: 'doc4'},
{siteTitle : 'Baidu Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'baidu.com', _id: 'doc5'},
{siteTitle : 'Hacker News', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'ycombinator.com', _id: 'doc6'},
{siteTitle : 'Mozilla foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'mozilla.org', _id: 'doc7'},
{siteTitle : 'Android foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'android.com', _id: 'doc8'},
{siteTitle : 'Wikipedia foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'wikipedia.org', _id: 'doc9'},
{siteTitle : 'Twitter foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'twitter.com', _id: 'doc10'},
{siteTitle : 'Facebook foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'facebook.com', _id: 'doc11'},
{siteTitle : 'Reddit foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'reddit.com', _id: 'doc12'},
{siteTitle : 'The Verge foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'theverge.com', _id: 'doc13'},
{siteTitle : 'Dropbox foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'dropbox.com', _id: 'doc14'},
{siteTitle : 'Microsoft foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'microsoft.com', _id: 'doc15'}
]);
}).then(function() {
return testDB.createIndex({
index: {
fields: ['dateCreated', 'domain'],
name: 'indexFoo'
}
});
}).then(function(result){
console.log(result);
}).then(function(){
return testDB.getIndexes();
}).then(function(result) {
console.log(JSON.stringify(result));
}).then(function(result){
return testDB.find({
selector: {
dateCreated: {'$exists': true}
},
use_index: 'indexFoo',
sort: [{dateCreated: 'desc'}]
});
}).then(function(result) {
console.log(result);
}).then(function(result) {
console.log(result);
}).catch(function (err) {
console.error(err);
console.log(err.message);
});
А вот живая версия: https://jsbin.com/qimulecezi/8/edit?js,console
Насколько я могу сказать, что я создания индекса так же, как это показано здесь: https://github.com/nolanlawson/pouchdb-find#dbcreateindexindex--callback и мое использование find с сортировкой довольно много дословно: https://github.com/nolanlawson/pouchdb-find#sorting