2017-02-23 68 views
0

我在pouchdb中有一些数据,我想要显示pouchdb中的总行数,我该怎么做?显示pouchdb中的总行数

angular.module("pouchapp", ["ui.router"]) 

.run(function($pouchDB) { 
    $pouchDB.setDatabase("dbinfo"); 
    //console.log($pouchDB.length); //show total rows in pouchdb 
}); 

.service("$pouchDB", ["$rootScope", "$q", function($rootScope, $q) { 
    var database; 

    this.setDatabase = function(databaseName) { 
     database = new PouchDB(databaseName); 
    } 
}]); 

回答

1

使用promises和allDocs函数,在Typescript中获取文档数量可能看起来像这样。我假设$pouchDB变量保存你的PouchDB。

$pouchDB.allDocs().then(entries => console.log(entries.rows.length)); 

一个纯JavaScript和回调的解决办法是这样的:

$pouchDB.allDocs(function(err, response) { 
    if (err) { return console.log(err); } 
    console.log(response.rows.length); 
}); 

注意:请参阅docs有关allDocs功能的更多信息。

如果您确实想要获取文档,请务必使用include_docs参数调用allDocs函数。默认情况下,您只能获得_id_rev属性,而不是整个文档。所以实际提取文件可能看起来像这样:

$pouchDB.allDocs({ 
    include_docs: true 
}).then(entries => console.log(entries)); 
+0

但在angularjs他们拒绝接受。然后,我该如何解决这个问题? – penguinnnnn

+0

我添加了一个版本使用回调... – Phonolog

+0

谢谢!它工作! – penguinnnnn