0

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?

hhh
  • 1,913
  • 3
  • 27
  • 35

1 Answers1

1

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"]}
nlawson
  • 11,510
  • 4
  • 40
  • 50
  • Thanks! So when there is a 409 data is not stored. I imagine that it is impossible to recover it? – hhh Oct 22 '15 at 15:48
  • That's why it gives you a 409, yes, so you can work around it. You might also look into the pouchdb-upsert plugin if you want to avoid 409s. – nlawson Oct 23 '15 at 13:49