Getting 409 trying to insert attachment to existing doc via SG


#1

I am trying to add an attachment to an existing document. I am using the nano library to send the data and attachments, for later access via CBLite.

Those documents I created from the db.attachment.insert seem to be fine. However, now I need to add multiple attachments per doc, I am running into the 409 issue. Specifically I am getting:

409
Document exists

      try {
        let response = await db.getAsync(docId);
        rev = response._rev;
        console.log ('existing entry, rev:' + rev);
      } catch (error) {
        console.log ('new entry');
        let docRes = await db.insertAsync(word, docId);
        console.log (docRes);
        rev = docRes.rev;
      }

        //...
        var data = {};

        try {
          data = await fs.readFile(filePath);
        } catch (ex) {
          console.log ("error: "+filePath);
          console.log  (ex);
          continue;
        }

        if (data) {
          try {
            console.log ('inserting '+filename+' at rev', rev);
            let res = await db.attachment.insertAsync(docId, filename, data, 'audio/mpeg', {rev: rev});
            console.log('attachment inserted', res);
          } catch (error) {
            console.log (error.statusCode);
            console.log (error.message);
          }
        } else console.log('file data empty');


#2

I found the problem: the filename had # characters and so was causing problems, changing the name/removing the offending characters seems to have solved the issue.


#3

That sounds like something (the nano library?) isn’t URL-encoding the attachment name properly.


#4

I suspect thats been the issue. I’ve ended up renaming all the attachments anyway, to save possible headaches like this in future.