在PouchDB文档中添加附加附件

时间:2017-03-18 07:43:48

标签: javascript electron attachment pouchdb

我试图在我的电子应用程序中为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中数据的样子(by-sequence table,json row):

{...,"_attachments":{"testPicture.jpg":{"content_type":"image/jpeg","revpos":34,"length":357677,"digest":"md5-Bjqd6RHsvlCsDkBKe0r7bg=="}}}

这里的问题是如何向结构添加另一个附件。不知何故,我无法通过putAttachment

实现这一目标

2 个答案:

答案 0 :(得分:1)

put取代了该文件。如果要在不覆盖其内容的情况下向现有文档添加附件,则应使用putAttachment

答案 1 :(得分:1)

您的问题,特别是代码很难阅读,因此错误并不容易发现:您没有等待承诺得到解决。使用修订版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提供的抽象。

也不要过多地分析底层数据结构,因为根据所使用的存储适配器,数据存储可能会有很大差异。不同的适配器如何存储数据非常有趣,但从不依赖于您发现的任何内容 - 数据格式可能会发生变化。

相关问题