【问题标题】:Adding attachments in couchdb using an update function使用更新功能在 couchdb 中添加附件
【发布时间】:2014-06-08 15:44:54
【问题描述】:

如何使用更新功能将附件添加到 couchdb?在我的 couchapp 中,我希望与数据库的所有交互都通过显示、列出和更新功能,以提高安全性。

我认为我可以使用更新功能删除附件(尽管我认为我不应该直接更新 _attachments 字段)。我正在使用:

function(doc, req){
    if (doc) {
        delete doc._attachments[req.form.filename];
        return [doc, JSON.stringify(doc)];
    }
    else {
        return [null, "Document does not exist."];
    }
}

谢谢

【问题讨论】:

    标签: couchdb attachment


    【解决方案1】:

    您可以在更新功能中轻松添加新附件 - 只需 inline 将它们添加到文档正文中:

    function(doc, req){
        if (doc) {
            // attachment delete
            delete doc._attachments[req.form.filename];
            // add another one
            doc._attachments.hello = {
                "content_type": "text/plain", // required
                "data": "d29ybGQ=" //world
            }
            return [doc, JSON.stringify(doc)];
        }
        else {
            return [null, "Document does not exist."];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 2018-04-26
      • 2015-10-29
      相关资源
      最近更新 更多