Pouchdb how can I putAttachment in a for loop?

688 Views Asked by At

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

There are 2 best solutions below

6
winhowes On BEST ANSWER

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.

0
nlawson On

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.