【问题标题】:409 error conflict update, updating pouchDB doc409 错误冲突更新,更新 pouchDB 文档
【发布时间】:2016-07-26 19:04:42
【问题描述】:

我的 pouchDB 代码尝试更新数据库中的文档时发生了奇怪的冲突

代码:

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));
                });

 }

在哪里:

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

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

        return usersDatabaseRemote;
    }
])

导致:

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

但正如您从代码中看到的那样,我从远程文档中获取了修订号 rev,所以我不明白为什么会有问题。

谢谢

【问题讨论】:

  • 您是否连续两次调用函数“addToExistingUser”?冲突可能是由于对文档的异步调用而发生的。
  • 准确!你是对的@AlexisCôté。

标签: angularjs ionic-framework couchdb pouchdb


【解决方案1】:

信用:@AlexisCôté

我多次调用更新远程文档的异步函数

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

这弄乱了 ._rev 数字。

所以现在我在一个对象中同时处理所有参数:

pouchDBservice.addObjectToExistingUser(userr._id, objectToAdd);

与:

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));
                });
        };

【讨论】:

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