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](https://cdn.wikitechy.com/tutorial/pouchdb/syntax-re.png)
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);
}
});
Read Also
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](https://cdn.wikitechy.com/tutorial/pouchdb/get-attach.png)
PouchDB Retrieve Attachment
Retrieve Attachment from a Remote Document
Example
![PouchDB Retrieve Attachment](https://cdn.wikitechy.com/tutorial/pouchdb/pouchdb-retrieve-attachment-2.png)
PouchDB Retrieve Attachment
- Click on the "employees" database. You will see the documents within the database.
![PouchDB Retrieve Attachment](https://cdn.wikitechy.com/tutorial/pouchdb/pouchdb-retrieve-attachment-3.png)
PouchDB Retrieve Attachment
- You can see a document having id "001". Click on the id and you will find the attachment.
![PouchDB Retrieve Attachment](https://cdn.wikitechy.com/tutorial/pouchdb/get-attachment.png)
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](https://cdn.wikitechy.com/tutorial/pouchdb/remote-get-attach.png)
PouchDB Retrieve Attachment