简体   繁体   中英

PouchDB: no conflict

I use PouchDB to save user data from a website to CouchDB (on 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.

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. 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. 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"]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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