获取 PouchDB 附件?

Get PouchDB attachment?

我正在尝试创建一个可以提问和回答问题的网站。为此,我有两个文件,一个将问题发送到数据库,另一个输出问题所在的每个 docs 的附件。

Post.js :

db.post({
    title: 'question'
}).then(function (response) {
    console.log(response)
    q = response
}).catch(function (err) {
    console.log(err);
});

await sleep(100)
console.log(q.rev)

var attachment = new Blob([{
    "title" : title,
    "content" : content,
    "option" : option,
    "spec" : spécialisation,
    "year" : year,
    "date" : date}, {type: 'text/json'}]);
db.putAttachment(q.id.toString(),'qData', q.rev.toString(), attachment, 'text/json')
.then(function (result) {
    console.log(result)
})
.catch(function (err) {
    console.log(err);
});

和view.js:

var all = db.allDocs({
}).then(function (result) {
    console.log(JSON.stringify(result.rows))
    return result.rows
}).catch(function (err) {
    console.log(err);
});

但我无法让它工作,它没有输出任何错误,而且我收到的单个输出是一个没有任何附件的文档。 我的(可能是新手)错误是什么?

尝试将以下选项添加到您的 allDocs 调用中:

{
  include_docs: true,
  attachments: true
}

在这两个选项上引用 docs

options.include_docs: Include the document itself in each row in the doc field. Otherwise by default you only get the _id and _rev properties.

options.attachments: Include attachment data as base64-encoded string.

文档有一个完整的示例,它应该 return 您的附件作为 base64 字符串:

db.allDocs({
  include_docs: true,
  attachments: true
}).then(function (result) {
  // handle result
}).catch(function (err) {
  console.log(err);
});

注意:我看到你在使用 Blob。如果您更愿意将附件作为 Blob 而不是 base64 字符串,请查看此选项:

options.binary: Return attachment data as Blobs/Buffers, instead of as base64-encoded strings.