简体   繁体   English

PouchDB:没有冲突

[英]PouchDB: no conflict

I use PouchDB to save user data from a website to CouchDB (on IrisCouch). 我使用PouchDB将用户数据从网站保存到CouchDB(在IrisCouch上)。 Following a mistake, I pushed documents with already taken names. 犯了一个错误,我推送了已经取名的文档。 I tried to recover the conflicting files admitting that there was a conflict. 我试图恢复冲突的文件,承认存在冲突。

db.get('threshold0_1_3', {conflicts: true}).then(function (doc) {
  console.log(doc, doc._conflicts);
}).catch(function (err) {
  console.log(err)
});

But the doc._conflicts is null and the document don't have a "_conflicts" property. 但是doc._conflicts为null,并且文档没有“ _conflicts”属性。

So I tried to create a conflict with this code: 因此,我尝试与此代码产生冲突:

var db = new PouchDB("http://127.0.0.1:5984/test");
var data = {"_id": "testDoc", "count": 1};

db.put(data).then(function (response) {
  // handle response
}).catch(function (err) {
  console.log(err);
});

// modify document
data = {"_id": "testDoc", "count": 2};

db.put(data).then(function (response) {
  // handle response
}).catch(function (err) {
  console.log(err);
});

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

// Check if the doc contains ._conflicts
db.get('testDoc', {conflicts: true}).then(function (doc) {
  console.log(doc);
  console.log(doc._conflicts);
}).catch(function (err) {
  console.log(err)
});

// Output:
Object {_id: "testDoc", _rev: "1-74620ecf527d29daaab9c2b465fbce66", count: 1}
undefined

What I am missing here? 我在这里想念的是什么?

A 409 indicates that the put() was rejected because it would have created a conflict. 409表示put()被拒绝,因为它造成冲突。 To manually create conflicts (if you really want to), you would need to put() the same document twice in two different databases, and then replicate them. 要手动创建冲突(如果确实要创建冲突),则需要put()同一文档两次放入两个不同的数据库中,然后复制它们。 Here's an example script. 这是一个示例脚本。

var PouchDB = require('pouchdb');

var db1 = new PouchDB('foo');
var db2 = new PouchDB('bar');

db1.put({_id: 'doc'}).then(function () {
  return db2.put({_id: 'doc'});
}).then(function () {
  return db1.sync(db2);
}).then(function () {
  return db1.get('doc', {conflicts: true});
}).then(function (doc) {
  console.log(JSON.stringify(doc));
}).catch(console.log.bind(console));

This prints out: 打印输出:

{"_id":"doc","_rev":"1-94c8f741dd8ab2a80ff49b1335e805fb","_conflicts":["1-2e0e51eeaf0d9d67b8293f205b8e4a0c"]}

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

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