2016-09-27 95 views
0

堆栈:nodejs,backbone.js v1.3.3,underscore.js v1.8.3,jQuery v2.2.4,cordova v6.1.1,https://pouchdb.com/download.html v6.0.5,https://github.com/jo/backbone-pouch v ?, https://github.com/apache/cordova-plugin-devicePouchDB打电话获得409:文档更新冲突

我已经接受调查的待办事项-DB使用_id的记录和更新_rev属性,但我无法找到我的出路这些冲突的,因此所有的帮助表示赞赏:

在我TodoModel,我在做:

return Backbone.Model.extend({ 

     idAttribute: "_id", 
     db:   new PouchDB("todos-db"), 
     sync:  this.BackbonePouch.sync({ 
        db: this.db, 
        fetch: "query", 
        options: { 
         query: { 
          include_docs: true 
         } 
        } 
       }), 

     initialize:  function(){ 

        this.listenTo(Backbone, "updateRec", function(model){ 
         this.updateRec(model); 
        }); 
       }, 

     updateRec: function(revRec){ 

        var self = this; 
        console.log("revRec:", revRec); 
        this.db.get(revRec._id, { conflicts: true }).then(function(doc){ 
         console.log("doc:", doc); // doc has no conflicts 
         revRec._rev = doc._rev; 
         return self.db.put(revRec); 
        }).then(function(result){ 
         console.log("updateRec result:", result); 
         return result; 
        }).catch(function(err){ 
         // TODO: the above 'put' is throwing multiple confict errors, yet the db is updating after 7 tries 
         console.log(err); 
        }); 

       } 
    }); 

}); 

在日志中,revRec给我:

_id:"2016-09-27T16:45:34.297Z" 
_rev:"5-b554a2b4989efedac3ca3ccb679aa42e" 
completed:false 
delegatedTo:"Jen" 
due:"Friday" 
priority:"A" 
project:"home" 
task:"MBath concept" 

的 '放' 的结果给了我:

{ok: true, id: "2016-09-27T16:45:34.297Z", rev: "6-c6647848d965c35c7ac4f968cac7a91b"} 

和卡ERR是:

CustomPouchError {status: 409, name: "conflict", message: "Document update conflict", error: true} 

回答

0

你可能要考虑pouchdb-upsert或阅读conflicts

在您的代码中,我认为您的特定错误很可能源于多次请求同一文档的竞争条件(例如,如果用户点击的速度很快?)。 pouchdb-upsert会自动为您处理这些错误。