PouchDB将调用获取409:文档更新冲突

时间:2016-09-27 19:21:45

标签: backbone.js pouchdb

stack: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?,{{ 3}}

我使用_id轮询了todos-db以获取记录,并更新了_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"

'put'结果给了我:

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

并且捕获错误是:

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

1 个答案:

答案 0 :(得分:0)

您可能需要查看pouchdb-upsert或阅读conflicts

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