【问题标题】:PouchDB - Adding attachment to existing docPouchDB - 将附件添加到现有文档
【发布时间】:2018-10-11 10:17:15
【问题描述】:

是否可以在现有文档中添加附件?当我使用时:

db.putAttachment()

我收到一个冲突错误...

【问题讨论】:

    标签: pouchdb


    【解决方案1】:

    当您将附件附加到文档时,您仍然需要传入现有文档的rev,因为它被视为对文档的修改。

    【讨论】:

      【解决方案2】:

      我总是发现创建一个 addOrUpdate() 类型的函数来处理邮袋很有用。它基本上会尝试使用您传递的 id 找到一个条目,如果找不到,它将创建,否则将更新

      function addPouchDoc(documentId, jsonString) {
      var pouchDoc = {
          _id: documentId,
          "pouchContent": jsonString,
      };
      
      // var test = db.get(documenId, function(err, doc) { });  alert(test);
      db.put(pouchDoc, function callback(err, result) {
          if (!err) {
              console.log('Successfully added Entry!');
      
          } else {
              console.log(err);
          }
      
      });  
      }
      

      这是你应该经常调用的函数

      function addOrUpdatePouchDoc(documentId, jsonString) {
      
      
      var pouchDoc = {
          _id: documentId,
          "pouchContent": jsonString
      };
      
      
      db.get(documentId, function(err, resp) {
      
          console.log(err);
          if (err) {
              if (err.status = '404') {
                  // this means document is not found
                  addPouchDoc(documentId, jsonString);
      
              }
          } else {
              // document is found OR no error , find the revision and update it
              //**use db.putAttachment here**
      
              db.put({
                  _id: documentId,
                  _rev: resp._rev,
                  "pouchContent": jsonString,
              }, function(err, response) {
                  if (!err) {
                      console.log('Successfully posted a pouch entry!');
                  } else {
                      console.log(err);
                  }
      
              });
      
      
          }
      
      
      
      });  
      }
      

      【讨论】:

        【解决方案3】:

        您需要传递将放置附件的文档的_id_rev

         db.putAttachment(_id.toString(), file_name, _rev.toString(), qFile.data, type )
          .then((result) =>{
              console.log(result)
              }).catch((err) => {
              console.log(err)
          });

        其中qFile.data代表blob,在这种情况下是64位数据字符串,type代表mimetype,例如'image/png'或'text/json'等。

        https://pouchdb.com/api.html#save_attachment

        还有一个很好但过时的现场示例: https://pouchdb.com/guides/attachments.html

        【讨论】:

          猜你喜欢
          • 2017-08-09
          • 2011-07-16
          • 2010-10-27
          • 2014-08-01
          • 1970-01-01
          • 2019-06-08
          • 1970-01-01
          • 2015-12-21
          • 1970-01-01
          相关资源
          最近更新 更多