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