如何使用现有附件更新文档?
How to update a document with existing attachments?
我正在尝试更新文档的数据,而不是文档中现有的附加图像,除非用户选择了新图像。
如何复制现有图像并将其放入新文档对象中?
function updateInspection() {
console.log('updateInspection started');
var data = $('#inspectionForm').serializeArray().reduce(function(obj, item) {
obj[item.name] = item.value;
return obj;
}, {});
console.log(' docID: ' + data._id);
console.log(' docREV: ' + data._rev);
if (!$('input[type=file]')[0].files[0]) {
console.log(' Get doc Attachment First!');
local_db.get(data._id,{attachments: true}, function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log('doc._attachments: ' +doc._attachments.cover_image.content_type);
data._attachments = doc._attachments; // THIS IS NOT WORKING
}
});
}
if ($('input[type=file]')[0].files[0]) {
console.log(' Add/Update Cover Image Attachment!');
cover_image_name = $('input[type=file]')[0].files[0].name;
console.log(' cover_image_name: ' + cover_image_name);
var input = document.querySelector('input[type=file]');
var file = $('#coverImage').prop('files')[0];
data[ "_attachments" ] = {
"cover_image": {
"content_type": "image/jpg",
"data": file
}
};
}
local_db.put(data).then(function (result) {
// handle result
console.log('Successfully posted '+result.id);
console.log('New_rev '+result.rev);
$("input#_rev").val(result.rev);
}).catch(function (err) {
console.log(err);
});
}
如果您使用 {attachments: false}
调用 db.get()
,那么您可以 re-insert 同一文档,并根据需要进行任何 non-attachment 更改。附件将在 JSON 中标记为 stub:true
,PouchDB 将其解释为 "aha, I can just check the checksum, and I don't need to re-insert the whole attachment blob."
因此,在您的情况下,如果 blob 未更改,则只需执行 db.get('id', {attachments: false})
,然后 re-insert 具有更改的同一文档。
我正在尝试更新文档的数据,而不是文档中现有的附加图像,除非用户选择了新图像。
如何复制现有图像并将其放入新文档对象中?
function updateInspection() {
console.log('updateInspection started');
var data = $('#inspectionForm').serializeArray().reduce(function(obj, item) {
obj[item.name] = item.value;
return obj;
}, {});
console.log(' docID: ' + data._id);
console.log(' docREV: ' + data._rev);
if (!$('input[type=file]')[0].files[0]) {
console.log(' Get doc Attachment First!');
local_db.get(data._id,{attachments: true}, function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log('doc._attachments: ' +doc._attachments.cover_image.content_type);
data._attachments = doc._attachments; // THIS IS NOT WORKING
}
});
}
if ($('input[type=file]')[0].files[0]) {
console.log(' Add/Update Cover Image Attachment!');
cover_image_name = $('input[type=file]')[0].files[0].name;
console.log(' cover_image_name: ' + cover_image_name);
var input = document.querySelector('input[type=file]');
var file = $('#coverImage').prop('files')[0];
data[ "_attachments" ] = {
"cover_image": {
"content_type": "image/jpg",
"data": file
}
};
}
local_db.put(data).then(function (result) {
// handle result
console.log('Successfully posted '+result.id);
console.log('New_rev '+result.rev);
$("input#_rev").val(result.rev);
}).catch(function (err) {
console.log(err);
});
}
如果您使用 {attachments: false}
调用 db.get()
,那么您可以 re-insert 同一文档,并根据需要进行任何 non-attachment 更改。附件将在 JSON 中标记为 stub:true
,PouchDB 将其解释为 "aha, I can just check the checksum, and I don't need to re-insert the whole attachment blob."
因此,在您的情况下,如果 blob 未更改,则只需执行 db.get('id', {attachments: false})
,然后 re-insert 具有更改的同一文档。