Getting 409 trying to insert attachment to existing doc via Sync Gateway

115 Views Asked by At

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

I've confirmed that the current revision is being sent.

  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

There are 2 best solutions below

1
On BEST ANSWER

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

0
On

The documentation indicates that a 409 error is returned if :

409 Conflict – Document’s revision wasn’t specified or it’s not the latest

https://docs.couchdb.org/en/stable/api/document/attachments.html