我试图跟踪pouchdb复制时的更改以创建加载屏幕。我有几个数据库,每个数据库有2000个左右的文档,并且初始加载要花很长时间,所以我希望该应用程序向用户指示后台发生了什么。 //define vars var nameDB = "location"; var remoteDB = new PouchDB("http://localhost:5984/" + nameDB); var localDB = new PouchDB(nameDB); var localdocCount; var localdocCounter = 0; var remotedocCount; //wrap replication in reusable function function replicate(){ //replicate TO the server PouchDB.replicate(localDB,remoteDB, { continuous: false, attachments: true }, function(){ //on callback replicate FROM server PouchDB.replicate(remoteDB,localDB, { continuous: false, attachments: true }, function(){ //replicate FROM complete ); }); //compact local database to keep size down //all revisions are replicated to the server //no need to keep them locally localDB.compact(); } //get the remote db info first and see how many documents it has //remote update_seq how does it match up to localdb update_seq? remoteDB.info(function(err, info) { remotedocCount = info.doc_count; console.log(info); }); //get info for local db for comparison with remote localDB.info(function(err, info) { localdocCount = info.doc_count; //Actually run the replication now that we have the //remote and local db info replicate(); //track any changes to the localdb localDB.changes({ continuous: true, onChange: function(){ //change detected increment counter localdocCounter++; console.log("Downloaded " + (localdocCount + localdocCounter) + " of " + remotedocCount + " to " + nameDB); //localdocCount === The total Documents in the local database //localdocCounter === The changes that have been made since //remotedocCount === The amount of documents we need } }); });上面的代码是我试图跟踪从服务器下载到客户端的文档数量。上面的代码有效,我可以看到文档正在下载。但是,这仅考虑了首次加载情况,当用户更新文档时该怎么办?我认为这就是update_seq发挥作用的地方。但是我无法弄清楚如何使用update_seq准确了解需要进行的更改。因此,总而言之,我可以看到是否正在发生更改,但是无法看到如何获取应用程序考虑自身最新状态所需的全部更改。也许这种方法完全不可行? 最佳答案 所以值得注意的一件事 //on callback replicate FROM server PouchDB.replicate(remoteDB,localDB, { continuous: false, attachments: true }, function(){ //replicate FROM complete );不赞成使用 //on callback replicate FROM server PouchDB.replicate(remoteDB,localDB, { continuous: false, attachments: true, complete: function() { //replicate FROM complete } });update_seq不能用作文档数量的代理,它是从何处复制的标记,该位置可能与文档写入一致,也可能不一致,但是在复制的两面都给出了doc_count,并且好像您有一个相当准确的计数器?哦,我只是读了更多代码,您不需要调用更改,replicate的docs_written包含更多信息,我们可能希望将其自动化,这是一个相当普遍的要求对于超出首次加载的请求,似乎您将拥有一个UI,该UI只是期望一个很小的更改,除非您使用非常慢的复制批量编写。关于couchdb - 跟踪pouchdb中的复制/更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22195802/ 10-11 17:02