简体   繁体   English

409错误冲突更新,更新pouchDB文档

[英]409 error conflict update, updating pouchDB doc

I have a weird conflict with my pouchDB code trying to update a document in my database 我与试图更新数据库中的文档的pouchDB代码发生奇怪的冲突

code: 码:

this.addToExistingUser = function (docId, key, value) {
        usersDatabaseRemote
                .get(docId)
                .then(function (doc) {
                    doc[key] = value;
                    return usersDatabaseRemote.put(doc, docId, doc._rev);
                })
                .then(function () {
                    console.log('added field: ' + key + ' to doc ' + docId);
                })
                .catch(function (err) {
                    console.log("error from addToExistingUser:");
                    console.log(JSON.stringify(err));
                });

 }

where : 在哪里:

.factory('usersDatabaseRemote', [
    'pouchDB',
    function (pouchDB) {
        'use strict';

        var usersDatabaseRemote = pouchDB('https://id:pwd@id.cloudant.com/boardl_users');

        return usersDatabaseRemote;
    }
])

leads to : 导致 :

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

But as you can see from the code I take the revision number rev from the remote document so I don't see why is there a problem with this. 但是正如您从代码中看到的那样,我从远程文档中获取了修订号rev ,所以我不明白为什么会有这个问题。

Thanks 谢谢

credit: @AlexisCôté 信用:@AlexisCôté

I was calling several times the async function that updates the remote doc 我多次调用了更新远程文档的异步功能

pouchDBservice.addToExistingUser(userr._id, 'weight',     
pouchDBservice.addToExistingUser(userr._id, 'height', userHeight);
pouchDBservice.addToExistingUser(userr._id, 'technique', userTechnique);

and this was messing with the ._rev number. 这使._rev号混乱。

So now I am doing all the parameters at the same time in an object : 所以现在我在一个对象中同时做所有参数:

pouchDBservice.addObjectToExistingUser(userr._id, objectToAdd);

with : 与:

this.addObjectToExistingUser = function (docId, obj) {
            usersDatabaseRemote
                .get(docId)
                .then(function (doc) {
                    for (var key in obj) {
                        if (!obj.hasOwnProperty(key)) continue;
                        console.log(key, obj[key])
                        doc[key] = obj[key];
                    }
                    return usersDatabaseRemote.put(doc);
                })
                .then(function () {
                    console.log('addObjectToExistingUser added object: ' + JSON.stringify(obj) + ' to doc ' + docId);
                })
                .catch(function (err) {
                    console.log("error from addObjectToExistingUser:");
                    console.log(JSON.stringify(err));
                });
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM