Getting 409 trying to insert attachment to existing doc via SG

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:

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);

        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');

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.

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

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