我试图在我的电子应用程序的 PouchDB 中向我的文档添加额外的附件。但是我只能添加最后一个附件,旧附件被覆盖。
以下数据不会以添加新文件的方式进行修改:
_attachments":{"someFile.jpg":{"content_type":"image/jpeg","revpos":5,"length":38718,"digest":"md5-X+MOUwdHmNeORSl6xdtZUg=="}
我应该首先阅读文档,然后使用以下方法使用多个附件添加其他文件来重新创建它:
db.put({
_id: 'mydoc',
_attachments: {
'myattachment1.txt': {
content_type: 'text/plain',
data: blob1
},
'myattachment2.txt': {
content_type: 'text/plain',
data: blob2
},
'myattachment3.txt': {
content_type: 'text/plain',
data: blob3
},
// etc.
}
});
?
您可以在下面看到我尝试运行的部分代码,以检查是否可以将两个附件添加到一个文档中(实际上我尝试使用同一文件两次以简化测试):
pdb.putAttachment(id, name, rev, file, type).then(function (result) {
console.log("att saved:");
console.log(result);
}).catch(function (err) {
console.log(err);
});
var newFileName = "new" + name;
pdb.putAttachment(id, newFileName, rev, file, type).then(function (result) {
console.log("att saved 2:");
console.log(result);
}).catch(function (err) {
console.log(err);
});
结果是:
Object {ok: true, id: "1489351796004", rev: "28-a4c41eff6fbdde8a722a920c9d5a1390"}
id
:
"1489351796004"
ok
:
true
rev
:
"28-a4c41eff6fbdde8a722a920c9d5a1390"
CustomPouchError {status: 409, name: "conflict", message: "Document update conflict", error: true, id: "1489351796004"}
error
:
true
id
:
"1489351796004"
message
:
"Document update conflict"
name
:
"conflict"
status
:
409
看起来我不明白什么或者我不知道如何正确使用 putAttachment。
我还将添加 sqlite 中的数据是什么样子的(按顺序表,json 行):
{...,"_attachments":{"testPicture.jpg":{"content_type":"image/jpeg","revpos":34,"length":357677,"digest":"md5-Bjqd6RHsvlCsDkBKe0r7bg=="}}}
这里的问题是如何向结构添加另一个附件。不知何故,我无法通过 putAttachment 实现
你的问题,尤其是代码很难阅读,所以错误不是那么容易发现:你没有等待承诺得到解决。当您更新具有修订版 1 的文档时,您必须等待结果,从那里读取修订版,然后才写入第二个附件。这将是我(未经测试)对您的代码的看法:
pdb.putAttachment(id, name, rev, file, type)
.then(function (result) {
// Use the new revision here:
return putAttachment(id, newFileName, result.rev, file, type);
}).then(function (result) {
console.log(result);
}).catch(function (err) {
console.log(err);
});
如果您对它们进行正确编码,则可以一次添加两个附件,但您必须自己处理。我建议您不要这样做——最好使用 PouchDB 提供的抽象。
也不要过多地分析底层数据结构,因为根据所使用的存储适配器,数据存储可能会有很大差异。不同的适配器如何存储它们的数据非常有趣,但永远不要依赖你发现的任何东西——数据格式可能会改变。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句