【问题标题】:Get PouchDB attachment?获取 PouchDB 附件?
【发布时间】:2017-10-23 22:21:43
【问题描述】:

我正在尝试创建一个可以提问和回答问题的网站。为此,我有两个文件,一个将问题发送到数据库,另一个输出问题所在的每个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);
});

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

【问题讨论】:

    标签: javascript html database pouchdb


    【解决方案1】:

    尝试在您的allDocs 通话中添加以下选项:

    {
      include_docs: true,
      attachments: true
    }
    

    在这两个选项上引用docs

    options.include_docs: 在每一行包含文档本身 文档字段。否则默认情况下你只会得到 _id 和 _rev 属性。

    options.attachments:将附件数据包含为 base64 编码的字符串。

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

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

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

    options.binary:将附件数据作为 Blob/缓冲区返回,而不是作为 base64 编码的字符串。

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 2016-05-10
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多