【问题标题】:PouchDB put call getting 409: Document update conflictPouchDB put call 得到 409:文档更新冲突
【发布时间】:2016-09-29 02:39:44
【问题描述】:

stack:nodejs、backscore.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-device

我已经使用 _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}

【问题讨论】:

    标签: backbone.js pouchdb


    【解决方案1】:

    您可能想查看pouchdb-upsert 或阅读有关conflicts 的信息。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-17
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多