PouchDB Retrieve Attachment



PouchDB Retrieve Attachment

  • getAttachment() - This method is used to retrieve an attachment from PouchDB. This method always returns blob or buffer objects
 PouchDB Retrieve Attachment

PouchDB Retrieve Attachment

Syntax:

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

Example

  • Using getAttachment() method to retrieve an attachment att_1.txt from the document "002" from the database named "Last_Database".
//Requiring the package   
var PouchDB = require('PouchDB');  
  
//Creating the database object   
  
var db = new PouchDB('Last_Database');  
  
//Reading the Document   
db.get('002',{attachments: true}, function(err, doc) {   
   if (err) {   
      return console.log(err);   
   } else {   
      console.log(doc);   
   }   
});  

Save the file name as "read_attachment.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the javascript file using node.

node read_attachment.js  
 PouchDB Retrieve Attachment

PouchDB Retrieve Attachment

Retrieve Attachment from a Remote Document

Example

  • Additionally we can retrieve an attachment to a remotely stored server (CouchDB). For this purpose, You just have to pass the path to the database where you need to read your attachment in CouchDB
  • We have a database name "employees" stored on the CouchDB server.
 PouchDB Retrieve Attachment

PouchDB Retrieve Attachment

  • Click on the "employees" database. You will see the documents within the database.
 PouchDB Retrieve Attachment

PouchDB Retrieve Attachment

  • You can see a document having id "001". Click on the id and you will find the attachment.
 PouchDB Retrieve Attachment

PouchDB Retrieve Attachment

  • You can see it by using Node.js command prompt:
//Requiring the package   
var PouchDB = require('PouchDB');  
  
//Creating the database object   
var db = new PouchDB('http://localhost:5984/employees');  
  
//Retrieving an attachment from a document   
db.getAttachment('001', 'att_1.txt', function(err, blob_buffer) {   
   if (err) {   
      return console.log(err);   
   } else {   
      console.log(blob_buffer);   
   }   
});  

Save the file name as "Read_Remote_attachment.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the javascript file using node.

node Read_Remote_attachment.js  

Output:

 PouchDB Retrieve Attachment

PouchDB Retrieve Attachment



Related Searches to PouchDB Retrieve Attachment