PouchDB Retrieve Attachment

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

You have to pass the document id and attachment id to use this method. This method also accepts an optional callback function.

Syntax:


Retrieve Attachment Example

Let's use getAttachment() method to retrieve an attachment attachment_1.txt from the document "002" from the database named "Last_Database".

Save the above code in a file named "Read_Attachment.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

PouchDB Retrieve attachent 1

Retrieve Attachment from a Remote Document

You can also retrieve an attachment from a remotely stored server (CouchDB). You just have to pass the path to the database in CouchDB, which contains the document that that you want to read.

Example

We have a database name "employees" stored on the CouchDB server.

PouchDB Retrieve attachent 2

Click on the "employees" database. You will see the documents within the database.

PouchDB Retrieve attachent 3

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

PouchDB Retrieve attachent 4

You can see it by using Node.js command prompt:

Save the above code in a file named "Read_Remote_Attachment.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

Output:

PouchDB Retrieve attachent 5



Latest Courses