0

I want to add a few attachments, and I can add one successfully, but when in loop , it will just save one image: 5.png and get conflict error:

enter image description here

enter image description here

    function addNewDoc() {

        db.get('my0112doc', function(err, doc) {
            if (err) {
                return console.log(err);
            }
            // var blob = base64toBlob(imgSoucrce30m, 'image/png', 1024);
            var blob =imgSoucrce30m;
            var attachment = {
                content_type: 'image/png',
                data: blob
            }
            for (var i = 5; i >= 0; i--) {
                var nameImg=i+'.png';
                db.putAttachment('my0112doc', nameImg, doc._rev, blob, 'text/plain', function(err, res) {
                    if (err) {
                        return console.log(err);
                    }
                });
            }
        });
    }

====================new solution========================================

in my function , i have specified its revision _rev, but the conflict still occurs. I cannot understand why.

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

    function addNewDoc() {
        db.get('my0112doc', function(err, doc) {
            if (err) {
                return console.log(err);
            }
            // var blob = base64toBlob(imgSoucrce30m, 'image/png', 1024);
            var blob = imgSoucrce30m;
            addAttachment(5,doc._rev,blob);
        });
    }

    function addAttachment(counter,revId,blob) {
        var nameImg = counter + '.png';
        db.putAttachment('my0112doc', nameImg, revId, blob, 'text/plain', function(err, res) {
            if (err) {
                return console.log(err);
            }
            if (counter >= 0) {
                addAttachment(counter - 1,revId,blob);
            }
        });
    }
AdvancingEnemy
  • 382
  • 3
  • 20

2 Answers2

1

So the issue is that putAttachment is asynchronous. So you need to wait for it to resolve before you can put again. I'm not sure if it returns a promise or not, but if not you could do something like this:

function addAttachment(counter) {
    var nameImg= counter + '.png';
    db.putAttachment('my0112doc', nameImg, doc._rev, blob, 'text/plain', function(err, res) {
        if (err) {
            return console.log(err);
        }
        if (counter >= 0) {
            addAttachment(counter - 1);
        }
    });
}
addAttachment(5);

You could also do something with promises, but this just calls the next iteration when the previous put finishes. It stops once the counter is negative just like your for loop.

winhowes
  • 7,845
  • 5
  • 28
  • 39
  • thanks winhowes, your answer is very helpful for me , i tried it, still i get the conflict error, I have specified its revision _rev, i cannot understand why the conflict occurs – AdvancingEnemy May 11 '16 at 04:39
  • I noticed you're passing in the same revID everytime, but wouldn't that change every time it's updated? Is the revID returned in the `res` object? – winhowes May 11 '16 at 04:48
  • [This method will update an existing document to add the attachment, so it requires a rev if the document already exists. If the document doesn’t already exist, then this method will create an empty document containing the attachment.](https://pouchdb.com/api.html#save_attachment) . I do it follow the API, my doc._rev is from my doc object, it's like this: _rev: ""56-eb390d85ef90f92efdfc0cf458d2590b"" – AdvancingEnemy May 11 '16 at 05:07
  • Right, but I think the _rev changes every time you update the doc, but you're passing the same _rev. I think you should be looking at the `res` to get the new _rev perhaps? – winhowes May 11 '16 at 07:48
  • 1
    that's right, every time , i want to save my attachment, i should have to get revid first. Thanks man. :P – AdvancingEnemy May 11 '16 at 09:40
  • @AdvancingEnemy great would you mind marking this as the correct answer then? Thanks! – winhowes May 11 '16 at 10:13
0

You might want to try reading We have a problem with promises and in particular the section about "how do I use forEach() with promises?" You probably want to use Promise.all() since PouchDB's API returns promises.

nlawson
  • 11,510
  • 4
  • 40
  • 50