Node.js 使用nodejs向coach db中的文档添加附件

Node.js 使用nodejs向coach db中的文档添加附件,node.js,couchdb,couchdb-nano,Node.js,Couchdb,Couchdb Nano,我想更新couchdb中的现有文档。我有一个图像,我想将其添加到数据库中的现有文档中,而不会丢失previus字段 我在用nodejs和nano 坦克,这让我找到了正确的方向。最后,我是这样做的: db.get(id,{ revs_info: true }, function (error, objeto) { if(error) console.log('wrong id'); fs.readFile('image.jpg', function(err, data)

我想更新couchdb中的现有文档。我有一个图像,我想将其添加到数据库中的现有文档中,而不会丢失previus字段

我在用nodejs和nano

坦克,这让我找到了正确的方向。最后,我是这样做的:

db.get(id,{ revs_info: true }, function (error, objeto) {
    if(error)
       console.log('wrong id');
fs.readFile('image.jpg', function(err, data) 
    {
     if (!err) 
     {
        db.attachment.insert(id, 'imagen.jpg', data, 'image/jpg',{ rev: objeto._rev}, function(err, body) { 
             if (!err)
                console.log(body);
        });
     }
 });
 });

你的问题不是很清楚具体的问题。所以这里只是一些关于更新文档的一般指导

  • 在设计数据库时,请确保设置ID,而不是允许couchdb编辑它。这样,您可以在更新文档时直接访问它
  • 更新时,您需要证明您正在更新文档的最新版本。我通常首先检索文档,并确保您将插入的文档中有最新的“\u rev”
  • 最后,如果不同的进程在检索和更新文档之间编辑了文档,则更新可能会失败。因此,您应该捕获插入中的失败,并重复该过程,直到成功
也就是说,有两种方法可以存储图像:

  • 作为附件:我相信nano支持
    attachment.insert()
    attachment.get()
    函数
  • 作为参考:我通常宁愿将图像存储在别处,而只存储url或文件路径来访问它们。我没用过多少nano,但我相信你可以通过下面的操作来做到这一点

    doc = db.get(docname); // get the document with all existing elements 
    doc['mynewimage'] = myimageurl; // update the document with the new image
                                    // this assumes it's a dictionary
    db.insert(doc); // inserts the document with the correct _id (= docname)
                    // and _rev