Retrieve Attachment in PouchDB

PouchDB Retrieve Attachment
To retrieve an attachment from PouchDB the getAttachment() method is used. It returns blob or the buffer objects.

Syntax:

db.getAttachment( docId, attachmentId, [callback] );

Example:

  • Create a file named “RetrieveAttach.js” within a folder named “Examples”.
  • Add the below code to the file.

    RetrieveAttach.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    db.get('101',{attachments: true}, function(err, doc) {
    if (err) {
    return console.log(err);
    } else {
    console.log(doc);
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node RetrieveAttach.js
    

 

To retrieve Attachment from a Remote Document:

Instead of the database name, pass the path of the database to retrieve an attachment from CouchDB or remote server.
Example:

  • Create a file named “RetrieveRemoteAttach.js” within a folder named “Examples”.
  • Add the below code to the file.
    RetrieveRemoteAttach.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('http://localhost:5984/students');
    db.getAttachment('101', 'attach_2.txt', function(err, blob_buffer) {
    if (err) {
    return console.log(err);
    } else {
    console.log(blob_buffer);
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node RetrieveRemoteAttach.js